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.

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.

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.

Consider this:

scanf("%s", &tempString);
temp->artist = tempString;

The second argument to scanf 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.

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.

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.

Remove the & operator, tempString as such must be used in that scanf (tempString is a pointer, hence no need for &).

scanf("%s", [B]&[/B]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.

Thank you so much mitrmkar - problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.