Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Trying to make a linked list in C please help (http://www.go4expert.com/forums/trying-linked-list-c-help-t21616/)

Funzo 1Apr2010 14:54

Trying to make a linked list in C please help
 
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;

}


rjmeyers81 2Apr2010 06:14

Re: Trying to make a linked list in C please help
 
In memberInsert():

newMember->next = NULL;

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

Rob

virxen 2Apr2010 13:28

Re: Trying to make a linked list in C please help
 
send the whole code here
or send it with pm.


All times are GMT +5.5. The time now is 22:16.