anonymous
2008-08-16 17:00:32 UTC
So i'm having a bit of a problem with my program. Everytime I run it, I get a run-time error and crashes. I narrowed the problem down to my malloc declaration. I'm not sure if i'm using it right. I'm trying to copy a line of text from a file (the line only includes the word "apple") and copy it onto a double pointer FROM an array.
This is what I have:
#define BUFSIZE 512
int lengthOfLine = 0;
char line[BUFSIZE];
char **buffer = NULL;
fgets(line, BUFSIZE, fp);
lengthOfLine = strlen(line);
buffer = (char**)malloc(lengthOfLine * sizeof(char));
strcpy(*buffer, line);
printf("%s\n", *buffer);
free(buffer);
Am I doing anything wrong? Any help would be appreciated.
Thanks.