Sign in to follow this  
Stormtrooper

Error before string constant

Recommended Posts

Stormtrooper    100
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
haegarr    7372
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this