I have a segmentation fault in my program

Joined
Dec 26, 2007
Messages
7
Reaction score
0
some information about my program:
it supposed to be a music cd store. the store is managed by 2 dinamic pointers of cd and artist types and a linked list of songs. the program has 7 options:
in the 1st we get as an input a new cd.we get the cd name ,the artist name,the number of songs and there lenghts.afterwards all the dinamic arrays and the linked list would be dynamicly increased.in the end the cd's name should be printed.

the 2nd option-we search for a cd by it's name if it is found print exist.else print doesn't exist.

the 3rd-we get a song's name and search for the cd which contain him, if it is found print it's details.

the 4rt-we get a disc and the number of the song in the disc.if found print song's details.else print don't exise.

the 5th-list all cd's of an artist if found print names and details else doesn't exist.

the 6th-list all existing cd's.

the 7-exit.

all the inputs to the structures should be made by special function as I tried to do for the cd's.
now a little information about the structures:
cd-1)the name of the artist which is a pointer of type Artist*.
2)the name of the cd which is a pointer which will be dinamicly allocated because it's size is unknown.
3)a dinamic array of pointers to the songs which exist in the cd.
4)the number of songs which the cd contains.

song-1)song's name.
2)song's length which is made of two positive integers the minutes and the seconds.
3)a pointer to the cd which contain the song of type cd ( a song or an artist names are unique).
4)a pointer to the next song in the store of type song.

artist-1)the name of the artist which is a dinamic array.
2)a dinamic array of pointers to the cd's of the artist.
3)number of the artist's cd's in the store.

store-1)dinamic array of pointers to the cd's.
2)number of cd's in the store.
3)dinamic array of pointers to the artists.
4)number of artist in the store.
5)a pointer to the list of all the songs in the store a pointer to song.

at each step I am not allowed to use more memory space than I need.
the initialization should be done by functions. each time an input is pased all the structures should be initialized and changed.


I wrote a function which recieves variables by adress and assigns them with the input and afterwards I will initialize my structs using those variables.

Code:
# include <stdio.h>
# include <malloc.h>
# include <string.h>

# define ARR_SIZE 100


struct CD;
struct Song;
struct Artist;
struct Store;

typedef struct
{
	char *songName;
	unsigned int minutes;
	unsigned int seconds;
	struct CD *cdSource;
	struct Song *nextSong;
} Song;

typedef struct
{
	struct Artist *artist;
	char *cdTitle;
	struct Song **song;
	unsigned int songsNum;
} CD;

typedef struct
{
	char *artistName;
	struct CD **cd;
	int numOfCd;
} Artist;

typedef struct
{
	struct CD **cd;
	unsigned int cdTotal;
	struct Artist **artist;
	unsigned int ArtistsNum;
	struct Song *song;	
} Store;

int get_input(char ***songLength,char ***title,char **cdTitle,char **artistName);
//void init_Cd(char **songLength,char **title,char *cdTitle,char *artistName,int songsTotal,Store *store,int *cdNum);

int main ()
{
	Store *store;
	int i,j,option,cdNumber=0,*cdNum,totalSongs;
	char **songLength=NULL,**title=NULL,*cdTitle,*artistName;

	cdNum=&cdNumber;
	
	
	while (option!=7)
	{	scanf("%d",&option);
	
		switch (option)
		{
			case 1: 
				totalSongs=get_input(&songLength,&title,&cdTitle,&artistName); 
				 for (i=0;i<totalSongs;i++)
        			 {       for (j=0;title[i][j]!='\0';j++)
                			{
                        			printf("%c",title[i][j]);  
                			}
                			printf("\n");
        			 }

				break;

			case 2:
				break;
		
			case 3:
				break;

			case 4:
				break;

			case 5:
				break;

			case 6:
				break;

			case 7:
				break;

			default:
				printf("error\n");
	
		}	 
	}	
	
	return 0;


}


int get_input(char ***songLength,char ***title,char **cdTitle,char **artistName)
{

	int stop,j=0,i,k,songsTotal;
	char temp[ARR_SIZE],temp2[100][100],temp1[100][100],enter;
	scanf("%c",&enter);
	gets(temp);
	*cdTitle=malloc(strlen(temp)*sizeof(char));
	strcpy(*cdTitle,temp);
//	puts(*cdTitle);
	temp[0]=NULL;	
	gets(temp);
        *artistName=malloc(strlen(temp)*sizeof(char));
        strcpy(*artistName,temp);
//	puts(*artistName);
	temp[0]=NULL;			
	scanf("%d",&songsTotal);	
//	printf("%d\n",songsTotal);
	*songLength=(char **) calloc(songsTotal, sizeof(char *));
	*title=(char **) calloc(songsTotal, sizeof(char *));
	stop=2*songsTotal;
	for (i=0;i<=stop;i++)
	{	
		gets(temp);
		if ((i%2)==0)
		{	*songLength[j]=(char *) malloc(strlen(temp)+1);
			for (k=0;k<=strlen(temp);k++)
				*songLength[j][k]=temp[k];
				*songLength[j][k]=0;	
			temp[0]=NULL;
			j++;
					
		}

		 else if ((i%2)==1)
		{	*title[i-j]=(char *) malloc(strlen(temp)+1); 
			for (k=0;k<=strlen(temp);k++)
                               *title[i-j][k]=temp[k];
                               *title[i-j][k]=0;
                               temp[0]=NULL;
		}
	}
				
	for (i=0;i<songsTotal;i++)
	{	for (j=0;*title[i][j]!='\0';j++)
		{
			printf("%c",*title[i][j]);
		}
		printf("\n");
	}

	return songsTotal;
}


My problem is that when I try to print title's value in the function it works fine ,however, when I try to print it's value in the main function I get the segmentation fault. the same problem I am haveing with songLength value's.
yet the other variables which are passed to the function initialized correctly.

How can I fix it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top