C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-07-2009, 08:38 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 22
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.
Roger is offline   Reply With Quote
Old 11-07-2009, 08:56 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Make a small working example.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct foo
{
    char name[BUFSIZ];
};
int main( void )
{
    struct foo *a, *b;
    a = malloc( sizeof *a );
    b = malloc( sizeof *b );
    if( a && b )
    {
        strcpy( a->name, "hello" );
        strcpy( b->name, "world" );

        if( strcmp( a->name, b->name ) == 0 )
        {
            printf( "\'%s\' is the same as \'%s\'\n", a->name, b->name );
        }
        else
        {
            printf( "\'%s\' is not the same as \'%s\'\n", a->name, b->name );
        }
    }
    free( a );
    free( b );

    return 0;
}

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-07-2009, 08:59 PM   #3
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 848
Code:
item * curr, * head, * tail, * new;
// ...
	 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){
Do you assign anything to "head"? From your code snippet, "head" is never set to anything, so youll get a problem when you access "head->name". Does it print "(null)" for the name of "head->name"?

Also, use "strncpy" instead of "strcpy" as to prevent overflows: strncpy - C++ Reference
nadroj is offline   Reply With Quote
Old 11-07-2009, 09:07 PM   #4
Registered User
 
Join Date: Oct 2009
Posts: 22
nadroj, head is NOT null ("John" and "Paul" is printed in my example), but I still get a segmentation fault on the next line.
Roger is offline   Reply With Quote
Old 11-07-2009, 09:08 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Show the whole function, not just the parts you feel like showing.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-07-2009, 09:12 PM   #6
Registered User
 
Join Date: Oct 2009
Posts: 22
Quote:
Originally Posted by quzah View Post
Show the whole function, not just the parts you feel like showing.

....edit

Last edited by Roger; 11-08-2009 at 06:14 PM.
Roger is offline   Reply With Quote
Old 11-07-2009, 09:22 PM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
You aren't assigning new->prev to anything. You need to always be assigning all pointers for a structure. Your first if doesn't assign prev.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-07-2009, 09:33 PM   #8
Registered User
 
Join Date: Oct 2009
Posts: 22
Quzah,
I don't understand why this works (which I put in just for testing):
Code:
printf("%d\n", strcmp(new->name, head->name));
and this very next line prints a "sementation fault" error:
Code:
if (strcmp(new->name, head->name) < 0){..
.
Roger is offline   Reply With Quote
Old 11-07-2009, 09:50 PM   #9
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
It doesn't really matter why one works and another doesn't. You're not setting your pointers up right, so if it "works", it's just you getting lucky.
Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

struct node {
   char name[30];
   char address[65];
   float amount;
   struct node * next;
   struct node * prev;
};
typedef struct node item;
item * curr, * head, * tail, * new;

void insert(char *name_, char *address_, float amount_){
    if ( (new = malloc( sizeof *new )) == NULL )
        return;
    strcpy( new->name, name_ );
    strcpy( new->address, address_ );
    new->amount = amount_;
    new->next = new->prev = NULL;

    if( head == NULL )
    {
        new-> prev = new->next = new;  /* I assume you want this circular. */
        head = tail = new;
    }
    else
    if( strcmp( new->name, head->name ) < 0 )
    {
        new->prev = head->prev;
        head->prev = new;
        new->next = head;
    }
    else
    if( strcmp( new->name, head->name ) > 0 )
    {
        new->next = head->next;
        new->prev = head;
        head->next = new;
    }
    else
    {
        free( new );
    }
}


int main() {
	insert("John", "12 main" , 112.2);
	insert("Paul", "14 oak", 200.5);
	
	curr = head;
	while(curr) {
		printf("%s, ", curr->name);
		printf("%s, ", curr->address);
		printf("%f\n", curr->amount);
		curr = curr->next;
   }

   return 0;
}

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-07-2009, 10:29 PM   #10
Registered User
 
Join Date: Oct 2009
Posts: 22
I appreciate your help, but it's not supposed to be circular,,, the reason for the "tail" and "previous" is to run backwards through the list starting at the tail; i.e., the list can be printed forwards from the head or backwards from the tail.

I'm trying to follow your code, but 1) I'm still a newb, and 2) getting tired and so I got confused with this line too:

Code:
if ( (new = malloc( sizeof *new )) == NULL )
        return;
Roger is offline   Reply With Quote
Old 11-07-2009, 10:45 PM   #11
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
It allocates the size of the structure, and if malloc fails, returns from the function. If you don't want it to be circular, remove the line that mentions it being circular.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
"Segmentation fault" lehe C Programming 5 04-10-2009 08:34 AM
Problem with a function ("Segmentation fault") kinghajj C Programming 6 02-21-2004 01:31 PM


All times are GMT -6. The time now is 03:47 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22