Trying to make a linked list in C please help

Discussion in 'C' started by Funzo, Apr 1, 2010.

  1. Funzo

    Funzo New Member

    Joined:
    Apr 1, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I've been wrestling with this for some hours and I'm wondering if anyone can see what's wrong with it.

    This is a linked list meaning the last element should always point to NULL


    Code:
    int main(void) {
    	srand ( time(NULL) );
    	BOOK books;				// Both of these should be stored as pointers and malloc should be used to make things easier for myself but when I switch it and make the necessary changes, the program still crashes
    	MEMBER club;
    	club.next = NULL;
    	books.ISBN[0] = 'a';
    	books.next = &books;
    ..........
    
    Code:
    if (userInput == 1) { // Let's add a new member!
    				char fName[32];
    				char mInit;
    				char lName[32];
    				char phoneNum[13];
    				printf("Please enter the user's first name\n");
    				getUserInputString(32, fName);  //this function works for sure
    				printf("Please enter the user's middle inital\n");
    				getUserInputString(1, &mInit);
    				printf("Please enter the user's last name\n");
    				getUserInputString(32, lName);
    				printf("Please enter the user's phone number\n");
    				getUserInputString(13, phoneNum);
    				memberInsert(&club, fName, mInit, lName, phoneNum);

    Code:
    MEMBER* memberInsert(MEMBER* memberStart, char fname[], char mInit, char lname[], char phonenum[]) {
    	MEMBER *newMember = (MEMBER*) malloc(sizeof(MEMBER));
    	strcpy((newMember->name).firstname, fname);
    	strcpy(newMember->name.lastname, lname);
    	newMember->name.intial = mInit;
    	strcpy(newMember->telephone, phonenum);
    	return insertMember(memberStart, newMember);
    }
    Code:
    MEMBER* insertMember(MEMBER *memberStart, MEMBER *newMember) {
    	MEMBER* currentMem = memberStart;
    	if ((memberStart->telephone)[0] == 'a') {
    		memberStart = newMember;
    		return memberStart;
    	}
    	while (currentMem->next != NULL)
    		currentMem = currentMem->next;
    	currentMem->next = newMember;
    	return memberStart;
    
    }
     
  2. rjmeyers81

    rjmeyers81 New Member

    Joined:
    Apr 2, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    In memberInsert():

    newMember->next = NULL;

    If that's not it, what behavior are you expecting and what is the result?

    Rob
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    send the whole code here
    or send it with pm.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice