Why won't this work?
2004-10-10 11:47:06 ET

#include <stdio.h>

int main() {
char *cstring;
FILE *fp;
fp = fopen( "proc.rc", "r" );
fscanf(fp,"%s", cstring);
printf("%s", cstring);

return 0;
}

It works on Utopia (Sun OS 5.7) and on Saturn (Linux 2.6.7) but it seg faults on artoo (FreeBSD 4.10) and on arcadia (NetBSD 1.6.1)

WTF?!


2004-10-10 12:08:10 ET

You never assign anything to *cstring. It is an uninitialized pointer. You need to allocate space that *cstring can then point to, either by defining it as a character array instead, or using dynamic memory management (malloc/free). That it is working on any system is a fluke.

2004-10-10 12:45:18 ET

That's almost what I thought! Thank's alot Moxie.

2004-10-10 12:54:00 ET

...and yes, it works when the char is intialized as an array.

2004-10-10 14:06:21 ET

I'm glad to help. :)

  Return to tetesuro's page