DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Printing struct from linked list (http://www.daniweb.com/forums/thread109887.html)

plgriffith Feb 19th, 2008 6:31 pm
Printing struct from linked list
 
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.

Aia Feb 19th, 2008 6:57 pm
Re: Printing struct from linked list
 
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.

plgriffith Feb 24th, 2008 11:09 pm
Re: Printing struct from linked list
 
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?

//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);

//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.

Dave Sinkula Feb 24th, 2008 11:56 pm
Re: Printing struct from linked list
 
Consider this:
  scanf("%s", &tempString);
  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
.

plgriffith Feb 25th, 2008 12:06 am
Re: Printing struct from linked list
 
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?

Dave Sinkula Feb 25th, 2008 12:08 am
Re: Printing struct from linked list
 
Either make them arrays in the structure, or after you've read a string into tempString,
malloc(strlen(tempString)+1)
to the pointer.

plgriffith Feb 25th, 2008 12:16 am
Re: Printing struct from linked list
 
  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.

mitrmkar Feb 25th, 2008 6:29 am
Re: Printing struct from linked list
 
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.
temp->artist = (char*) malloc(strlen(tempString)+1);
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.

plgriffith Feb 25th, 2008 9:39 am
Re: Printing struct from linked list
 
Thank you so much mitrmkar - problem solved.


All times are GMT -4. The time now is 1:49 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC