| | |
Printing struct from linked list
Thread Solved
![]() |
•
•
Join Date: Jan 2008
Posts: 97
Reputation:
Solved Threads: 6
I have a linked list of structs, with each struct having a char* and an int.
When I try to print the list I get a seg fault. If I just print the int however it works. What am I doing wrong so the char* won't print?
printf("%s %d\n", p->name, p->age);
Thanks.
When I try to print the list I get a seg fault. If I just print the int however it works. What am I doing wrong so the char* won't print?
printf("%s %d\n", p->name, p->age);
Thanks.
•
•
Join Date: Jan 2008
Posts: 97
Reputation:
Solved Threads: 6
After many tries I still have not figured out the problem. I no longer have a seg fault, but now when I try to print the list I get "garbage" values for the strings and the correct values for the ints. I have no idea why I am getting correct values for ints and not char *. Any ideas?
Thanks.
C Syntax (Toggle Plain Text)
//mp3.h #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char* artist; char* album; char* song; int date; //date the song is stored in the database int time; //in seconds int bitrate; //in kbps struct node* link; }; void addToList(struct node **q); void deleteFromList(struct node **q); void printList(struct node *q);
C Syntax (Toggle Plain Text)
//mp3.c #include "mp3.h" int main(void) { struct node *p; p = NULL; //so p is the head of an empty list int choice=4; while(choice != 0) { printf("0: Exit.\n"); printf("1: Add to the list.\n"); printf("2: Delete from the list.\n"); printf("3: Print the list.\n"); scanf("%d", &choice); switch(choice) { case 1: addToList(&p); break; case 2: deleteFromList(&p); break; case 3: printList(p); break; default: break; } } return 0; } void addToList(struct node** q) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); char tempString[80]; //this is the 4-line version that works /* printf("Enter a name: "); scanf("%s", &tempString); temp->artist = tempString; printf("%s", temp->artist); */ printf("Enter the name of the artist.\n"); scanf("%s", &tempString); temp->artist = tempString; printf("Enter the name of the album.\n"); scanf("%s", &tempString); temp->album = tempString; printf("Enter the name of the song.\n"); scanf("%s", &tempString); temp->song = tempString; printf("Enter the date (mmddyyyy).\n"); scanf("%d", &temp->date); printf("Enter the length of the song (in seconds).\n"); scanf("%d", &temp->time); printf("Enter the quality (bitrate) of the song.\n"); scanf("%d", &temp->bitrate); temp->link = *q; *q = temp; } void deleteFromList(struct node** q) { } void printList(struct node* q) { if(q == NULL) { printf("\n\nNo data.\n\n"); } else { printf("\n\n Artist Name | Album Name | Song Name | Entry Date | Length | Quality\n"); printf("----------------------------------------------------------------------\n"); while(q != NULL) { printf(" %11s %10s %9s %10d %6d %7d \n",q->artist,q->album,q->song,q->date,q->time,q->bitrate); q = q->link; } } }
Thanks.
Consider this:
The second argument to [icode]scanf[icode] is of the wrong type.
You don't copy the string with assignment, look into
C Syntax (Toggle Plain Text)
scanf("%s", &tempString); temp->artist = tempString;
temp->artist is a dangling pointer -- you need to allocate memory for it.You don't copy the string with assignment, look into
strcpy . "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jan 2008
Posts: 97
Reputation:
Solved Threads: 6
Thanks for the response. I tried strcpy before, but that lead me to a seg fault. I'm sorry, how would I go about allocating memory for the pointer?
Either make them arrays in the structure, or after you've read a string into tempString,
malloc(strlen(tempString)+1) to the pointer. Last edited by Dave Sinkula; Feb 25th, 2008 at 12:08 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jan 2008
Posts: 97
Reputation:
Solved Threads: 6
C Syntax (Toggle Plain Text)
scanf("%s", &tempString); strcpy(temp->artist, tempString); temp->artist = (char*) malloc(strlen(tempString)+1);
Is this what you mean?
I would make them arrays in the structure but the assignment specifies that they have to be char*s.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
Remove the & operator, tempString as such must be used in that scanf (tempString is a pointer, hence no need for &).
Then allocate memory before copying.
You may want to check that malloc() actually succeeded, (i.e NULL != temp->artist). Also remember to free() all the malloc'ed memory at some point.
scanf("%s", &tempString); C Syntax (Toggle Plain Text)
temp->artist = (char*) malloc(strlen(tempString)+1); strcpy(temp->artist, tempString);
![]() |
Similar Threads
- deleting and displaying in a linked list (C++)
- NEED help pls. about linked list (C++)
- linked list problem!!! (C)
- Link-listed addressbook (C)
- 500 ways to print [1..10]! (IT Professionals' Lounge)
- Help with Linked Lists with digits (C++)
- Sorted linked list help (C++)
- more linked list printing problems (C)
- File writing Error (C)
- printing a linked list (C++)
Other Threads in the C Forum
- Previous Thread: help to give program for this output?
- Next Thread: Please help. how do i divide by shifting to the right :S?
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate lowest match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






