Thread: struct, string, strcmp(), and "segmentation fault"

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    30

    struct, string, strcmp(), and "segmentation fault"

    I'm getting a segmentation fault on the last line noted below. I'm comparing two strings.
    Code:
    struct node {
       char name[25];
       char address[65];
       float amount;
       struct node * next;
    };
    typedef struct node item;
    item * curr, * head, * tail, * new;
    
    
    void insert(char *name_, char *address_, float amount_){
    	
    	new = (item *)malloc(sizeof(item));
    	
    	strcpy (new->name, name_ );
    	strcpy (new->address, address_ );
        	new->amount = amount_;
    
    ........................
    
    	 printf("%s, ", new->name); printf("%s\n, ", head->name);
             //the above print statements where put in as tests. They print two names.   
             //But, the very next line below produces an error: "segmentation fault"
    	if (strcmp(new->name, head->name) < 0){
    ...
    ...
    Last edited by Roger; 11-07-2009 at 10:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Segmentation fault"
    By lehe in forum C Programming
    Replies: 5
    Last Post: 04-10-2009, 08:34 AM
  2. Problem with a function ("Segmentation fault")
    By kinghajj in forum C Programming
    Replies: 6
    Last Post: 02-21-2004, 01:31 PM