[ back to toc ]

Segmentation Fault Core Dumped

Date: 2002/04/15 11:13

Q:
Hi

I keep getting segmentation fault core dumped error. Am I making a mistake
in allocating memory?

I need to copy the string token into
command.argv[j]

These are declared as
char *token;
and

struct command_t {
char *name;
int argc;
char *argv[];
} command;

Am I making a mistake in the following statements??
command.argv[j] = (char *)malloc(strlen(token)+1);
strcpy (token, command.argv[j]);

I am programming in C on the UNIX platform.

Thanks
Maharajan
A:
Yes. The issue is that before allocating memory

command.argv[j] = (char *)malloc(strlen(token)+1);

you have to allocate memory for the array. You also have to change the
declaration

char *argv[];

to

char **argv;

regards,
Peter

[ back to toc ]