Printing struct from linked list

Thread Solved
Reply

Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Printing struct from linked list

 
0
  #1
Feb 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,004
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 172
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Printing struct from linked list

 
0
  #2
Feb 19th, 2008
A segmentation fault occurs when a program tries to access memory location that is not allow to play with.

My guess is that your char* is pointing to some wrong memory location. Maybe you haven't initialized to point to a string.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Re: Printing struct from linked list

 
0
  #3
Feb 24th, 2008
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?

  1. //mp3.h
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. struct node
  8. {
  9. char* artist;
  10. char* album;
  11. char* song;
  12. int date; //date the song is stored in the database
  13. int time; //in seconds
  14. int bitrate; //in kbps
  15. struct node* link;
  16. };
  17.  
  18.  
  19. void addToList(struct node **q);
  20. void deleteFromList(struct node **q);
  21. void printList(struct node *q);

  1. //mp3.c
  2.  
  3. #include "mp3.h"
  4.  
  5.  
  6. int main(void)
  7. {
  8. struct node *p;
  9. p = NULL; //so p is the head of an empty list
  10. int choice=4;
  11.  
  12.  
  13. while(choice != 0)
  14. {
  15. printf("0: Exit.\n");
  16. printf("1: Add to the list.\n");
  17. printf("2: Delete from the list.\n");
  18. printf("3: Print the list.\n");
  19. scanf("%d", &choice);
  20.  
  21. switch(choice)
  22. {
  23. case 1:
  24. addToList(&p);
  25. break;
  26. case 2:
  27. deleteFromList(&p);
  28. break;
  29. case 3:
  30. printList(p);
  31. break;
  32. default:
  33. break;
  34. }
  35.  
  36. }
  37.  
  38. return 0;
  39. }
  40.  
  41. void addToList(struct node** q)
  42. {
  43. struct node *temp;
  44. temp=(struct node *)malloc(sizeof(struct node));
  45. char tempString[80];
  46.  
  47. //this is the 4-line version that works
  48. /*
  49.   printf("Enter a name: ");
  50.   scanf("%s", &tempString);
  51.   temp->artist = tempString;
  52.   printf("%s", temp->artist);
  53.   */
  54.  
  55.  
  56. printf("Enter the name of the artist.\n");
  57. scanf("%s", &tempString);
  58. temp->artist = tempString;
  59.  
  60. printf("Enter the name of the album.\n");
  61. scanf("%s", &tempString);
  62. temp->album = tempString;
  63. printf("Enter the name of the song.\n");
  64. scanf("%s", &tempString);
  65. temp->song = tempString;
  66. printf("Enter the date (mmddyyyy).\n");
  67. scanf("%d", &temp->date);
  68. printf("Enter the length of the song (in seconds).\n");
  69. scanf("%d", &temp->time);
  70. printf("Enter the quality (bitrate) of the song.\n");
  71. scanf("%d", &temp->bitrate);
  72.  
  73.  
  74. temp->link = *q;
  75. *q = temp;
  76. }
  77.  
  78. void deleteFromList(struct node** q)
  79. {
  80.  
  81.  
  82. }
  83.  
  84. void printList(struct node* q)
  85. {
  86.  
  87. if(q == NULL)
  88. {
  89. printf("\n\nNo data.\n\n");
  90. }
  91. else
  92. {
  93. printf("\n\n Artist Name | Album Name | Song Name | Entry Date | Length | Quality\n");
  94. printf("----------------------------------------------------------------------\n");
  95. while(q != NULL)
  96. {
  97. printf(" %11s %10s %9s %10d %6d %7d \n",q->artist,q->album,q->song,q->date,q->time,q->bitrate);
  98. q = q->link;
  99. }
  100. }
  101.  
  102. }

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Printing struct from linked list

 
0
  #4
Feb 24th, 2008
Consider this:
  1. scanf("%s", &tempString);
  2. temp->artist = tempString;
The second argument to [icode]scanf[icode] is of the wrong type.
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Re: Printing struct from linked list

 
0
  #5
Feb 25th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Printing struct from linked list

 
0
  #6
Feb 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Re: Printing struct from linked list

 
0
  #7
Feb 25th, 2008
  1. scanf("%s", &tempString);
  2. strcpy(temp->artist, tempString);
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Printing struct from linked list

 
1
  #8
Feb 25th, 2008
Remove the & operator, tempString as such must be used in that scanf (tempString is a pointer, hence no need for &).
scanf("%s", &tempString);
Then allocate memory before copying.
  1. temp->artist = (char*) malloc(strlen(tempString)+1);
  2. strcpy(temp->artist, tempString);
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Re: Printing struct from linked list

 
0
  #9
Feb 25th, 2008
Thank you so much mitrmkar - problem solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC