![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 22
| struct, string, strcmp(), and "segmentation fault" 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 | |
| | #2 |
| +++ OK NO CARRIER 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 | |
| | #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){
Also, use "strncpy" instead of "strcpy" as to prevent overflows: strncpy - C++ Reference |
| nadroj is offline | |
| | #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 | |
| | #5 |
| +++ OK NO CARRIER 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 | |
| | #6 | |
| Registered User Join Date: Oct 2009
Posts: 22
| Quote:
....edit Last edited by Roger; 11-08-2009 at 06:14 PM. | |
| Roger is offline | |
| | #7 |
| +++ OK NO CARRIER 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 | |
| | #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));
Code: if (strcmp(new->name, head->name) < 0){..
|
| Roger is offline | |
| | #9 |
| +++ OK NO CARRIER 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 | |
| | #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 | |
| | #11 |
| +++ OK NO CARRIER 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |