0

my code is the following: it asks the user to input name of planet, distance, and a description. Then it will print out whatever the user entered.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* planet type */
typedef struct{
  char name[128];
  double dist;
  char description[1024];
} planet_t;

/* my planet_node */
typedef struct planet_node {
  char name[128];
  double dist;
  char description[1024];
  struct planet_node *next;
} planet_node;

/* the print function, not sure if its correct */
void print_planet_list(planet_node *ptr){
  while(ptr != NULL){
    printf("%s %lf %s\n", ptr->name, ptr->dist, ptr->description);
    ptr=ptr->next;
  }
  printf("\n");
}

int main(){
  char buf[64];
  planet_node *head = NULL;
  int quit = 0;

  do {
    printf("Enter planet name (q quits): ");
    scanf("%s",buf);
    if((strcmp(buf,"q")==0)){
      quit = 1;
    }
    else{
      planet_node *new = malloc(sizeof(planet_node));       /* New node */
      strcpy((*new).name, buf);         /* Copy name */
      printf("Enter distance and description: ");
      scanf(" %lf ", new->dist); /* Read a new distance into pluto's */
      gets(new->description);      /* Read a new description */
      new->next = head;        /* Link new node to head */
      head=new;        /* Set the head to the new node */
    }
  } while(!quit);

  printf("Final list of planets:\n");
  print_planet_list(head);


  while(head != NULL){
    planet_node *remove = head;
    head = (*head).next;
    free(remove);
  }
}

the places where i made comments are the where i am not sure if it is correct, the code complies but gives me a segmentation error. any help? thanks.

5
  • 1
    Did you compile your code with all warnings and debugging information (on Linux use gcc -Wall -g)? Then improve the code till no warnings are given. At last use a debugger (gdb on Linux) and a leak detector (valgrind on Linux) to debug. Commented Oct 23, 2012 at 18:43
  • I don't get an error in VC (compile or run). Though I did change "new" - that's a keyword in C++. Commented Oct 23, 2012 at 18:48
  • @MarkStevens but not in C. So it compiles fine if you use .c extension. Commented Oct 23, 2012 at 18:49
  • @PrototypeStark: I know, just feels funny to call a variable "new" ;) Commented Oct 23, 2012 at 18:50
  • @MarkStevens Yeah I know ;) :P Commented Oct 23, 2012 at 18:52

1 Answer 1

3

scanf(" %lf ", new->dist);

This is where your error is. It should be:

scanf(" %lf ", &(new->dist));

Also an extra ' ' causes it to accept extra characters. This can be avoided with

scanf("%lf", &(new->dist));

Sign up to request clarification or add additional context in comments.

4 Comments

Notice that this error would have been caught by gcc -Wall which issues a warning.
@BasileStarynkevitch unfortunately I don't have gcc, but I caught it at first reading. :P. Compiler in my head is slower but more accurate(i think) :D
Just for reference, the actual warning is "test.c:44:7: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 2 has type ‘double’ [-Wformat] test.c:60:1: warning: control reaches end of non-void function [-Wreturn-type]"
Thanks @harpun I am seriously considering installing cygwin and gcc/g++. Its more in use on SO these days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.