Anubhav Hanjura wrote:

> If I have the following code:

> --------------
> main()
> {
> 
>     char *filename;
>     char *string = "this is a string";
> 
>     strcpy(filename,string);
>     printf("filename is %s\n",filename);
> }
> --------------
> 
> it gives me a segmentation fault when I compile it using one c++
> compiler while on other it works fine.Why???

Because the initial value of `filename' is random. If it happens to
point to writable memory, then it won't segfault, otherwise it will.

You need to either initialise `filename' to point to some writable
memory (e.g. to an array, or to the result of malloc(), alloca() etc),
or make it an array.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to