• Advertisement
Sign in to follow this  

Error before string constant

This topic is 3348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

const char *PATH;

#ifdef (__APPLE__) || defined(__LINUX__)
#include <stdlib.h>
sprintf(PATH, "%s/", getenv("HOME"));
#else
sprintf(PATH, "%s/", getCwd());
#endif
I'm getting this error on the first sprintf, and I have no idea what this error means. error: syntax error before string constant Could the error be any more vague? :P

Share this post


Link to post
Share on other sites
Advertisement
An #include like the shown one should appear outside of any function body, while a function invocation like sprintf(...) should appear inside a function body. So you may try a layout like:
#ifdef (__APPLE__) || defined(__LINUX__)
#include <stdlib.h>
#endif

some_return_type some_function_name() {
#ifdef (__APPLE__) || defined(__LINUX__)
sprintf(PATH, "%s/", getenv("HOME"));
#else
sprintf(PATH, "%s/", getCwd());
#endif
}

Make sure, of course, that PATH is allocated when sprintf is invoked.
At last, a "const char*" PATH is used for writing. Make it a "char* PATH" instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement