I have this struct
typedef struct no
{
char command[MAX_COMMAND_LINE_SIZE];
struct no * prox;
} lista;
lista *listaCommand = NULL;
and I'm filling listaCommand with a simple function that seems to work ok, as I can read the values without any problem, but if I try to compare, like
strcmp(listaCommand->prox>command, ">")
I just get a segmentation fault, even though the value > is there, why this is happening?
strcmp(listaCommand->prox>command, ">")
Should be
strcmp(listaCommand->prox->command, ">")
In your code listaCommand->prox>command
will be seen as a comparison operation, using the >
operator. A comparison in C returns an integer, 0 if false, non-zero otherwise. There are good chances it will return 0
, which is not a valid memory address. Hence, the segmentation fault.
Change
strcmp(listaCommand->prox>command, ">")
to
strcmp(listaCommand->prox->command, ">")
Allocate memory !!!
typedef struct no
{
char str[20];
struct no * prox;
} lista;
lista *listaCommand = NULL;
int main(int argc, char** argv)
{
listaCommand = malloc(sizeof(lista));
listaCommand->prox = malloc(sizeof(lista));
strcpy(listaCommand->prox->str, "aaa");
printf("%d\n", strcmp(listaCommand->prox->str, ">>"));
free(listaCommand->prox);
free(listaCommand);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.