1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Trying to make a linked list in C please help

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

  1. Funzo

    Funzo New Member

    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

    In memberInsert():

    newMember->next = NULL;

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

    Rob
     
  3. virxen

    virxen New Member

    send the whole code here
    or send it with pm.
     

Share This Page