Hi I'm currently working on a college programming assignment and for this assignment I need to create a contact list of first name and last name and store them as the program runs
I may not be able to explain this well but I understand what i have to do and i feel like my problem is just basic C logic that i cant quite wrap my head around
so i have
struct contact{//initialize the structure contact
char *firstname;
char *lastname;
struct contact *nextptr;
};
where nextptr points to a new contact stucture
I start a while loop and, I prompt the user for the first and last name and store that in newptr
then i set the contact list as a whole as listStart=newptr;
now the while loop starts over and newptr gets reset to NULL
now i want to set listStart->nextptr=newptr; which i understand
now the while loop loops and now i want to set listStart->nextptr->nextptr=newptr;
How can I do this without having to type ->nextptr after every loop? and keep adding a ->nextptr for ever how many loops i want?
please let me know if theres any questions. Thanks to anyone that can help, here's the code that i'm working on.
struct contact{//initialize the structure contact
char *firstname;
char *lastname;
struct contact *nextptr;
};
//initialize subroutines
void PrintContact(struct contact *listStartPtr);
int main(){
int i=0;
char bufferFirstName[200];
char bufferLastName[200];
struct contact *listStartPtr=NULL;
struct contact *newptr=NULL;
while(i==0){
newptr=(struct contact *)malloc(sizeof(struct contact));
if(newptr==NULL){
printf("Couldn't allocate memory");
}
printf("Please enter your first name or enter 'done' to exit\n:");
scanf("%s",bufferFirstName);
if(strcmp (bufferFirstName,"done") ==0 ){;
i=1;
}
if(i!=1){
newptr->firstname=malloc(sizeof(strlen(bufferFirstName)));
if(newptr->firstname==NULL){
printf("Couldn't allocate memory\n");
}
strcpy(newptr->firstname,bufferFirstName);
printf("\nPlease enter your last name\n:");
scanf("%s",bufferLastName);
printf("\n");
newptr->lastname=malloc(sizeof(strlen(bufferLastName)));
if(newptr->lastname==NULL){
printf("Couldn't allocate memory\n");
}
strcpy(newptr->lastname,bufferLastName);
if(listStartPtr==NULL){
listStartPtr=newptr;
}
listStartPtr=newptr;//first run through
listStartPtr->nextptr=newptr;//second loop
listStartPtr->nextptr->nextptr=newptr;//third loop
listStartPtr->nextptr->nextptr-nextptr=newptr;//fourth
//How would I keep going for the nth amount of loops?
}
}