Does the order I declare pointers really matter in C? getcwd() problem...

Posted by chucknelson on Stack Overflow See other posts from Stack Overflow or by chucknelson
Published on 2010-04-12T00:40:48Z Indexed on 2010/04/12 0:43 UTC
Read the original article Hit count: 293

Filed under:
|
|

On a Solaris 5.8 machine, I have the following code:

[non-working code]

char *buf;
char *dir;
size_t psize;

psize = (size_t) 1024;
dir = getcwd(buf, psize);

On this unix machine, the above does not work and I get a segmentation fault when trying to run the program. It only works if I declare dir before buf:

[working code]

char *dir;
char *buf;
...
dir = getcwd(buf, psize);

When using another flavor of Unix, such as Mac OS X, I don't get any of these what seem to be very strict rules on how to write the code. Can anyone explain what's going on with the above example? Thanks!

© Stack Overflow or respective owner

Related posts about c

    Related posts about unix