Thread: What's wrong with my pointer?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    What's wrong with my pointer?

    Hi everyone. I'm using GCC to compile code according to the C99 standard.

    I'm using libcurl to make HTTP requests. I need to call the function curl_easy_setopt where the third parameter is a "pointer to a zero-terminated string." GCC chokes on the following code, claiming that for the third parameter: invalid application of 'sizeof' to incomplete type 'const char[]'.

    Note that if I put the szuseragent definition in the same translation unit, it works. Also, if I change szuseragent to &szuseragent[0], it works!

    Is it my fault or gcc's?

    Code:
    #include "Variables.h"
    
    int setuseragent ( void)
    {
         curl_easy_setopt ( curl , CURLOPT_USERAGENT , szuseragent ) ;
         return 0 ;
    }
    
    //Variables.h
    
    extern const char szuseragent [] ;
    
    //Variables.c
    
    const char szuseragent[]= "My user agent string." ;

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    From the error message, curl_easy_setopt() is a macro, and uses sizeof() on its third argument.

    The problem is with your code (or, more accurately, with youre code not matching expectations of the macro) not with gcc.

    That means szuseragent needs to be a string literal or an array of char for which the size is known at compile time. Among other things, that means there cannot be a separation of declaration (in Variables.h) and definition (in Variables.c).

    To fix the problem, change the declaration in Variables.h of szuseragent to
    Code:
    const char szuseragent[] = "My user agent string."
    and don't define szuseragent in Variables.c.

    Changing szuseragent to &szuseragent[0] will make your code compile, but - given what the documentation for libcurl says about the third argument of curl_easy_setopt() - it will probably misbehave at run time (as sizeof(szuseragent) and sizeof(&szuseragent[0]) will not be different values).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    144
    Would a better solution be :

    Code:
    const char szuseragent [100] = "My user agent string." ;

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by grumpy View Post
    To fix the problem, change the declaration in Variables.h of szuseragent to
    Code:
    const char szuseragent[] = "My user agent string."
    and don't define szuseragent in Variables.c.
    But if variables.h is included in multiple translation units, won't there be multiple definitions for szusergent? That would result in multiple definition linker errors, right?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Richardcavell View Post
    Would a better solution be :

    Code:
    const char szuseragent [100] = "My user agent string." ;
    Only if you wish to allocate 100 characters where only 22 is needed (which I would summarise, as an answer to your question, as "No"). The compiler can work out the size of string literals, and therefore can work out the actual array size needed - if allowed to.

    Quote Originally Posted by pheininger View Post
    But if variables.h is included in multiple translation units, won't there be multiple definitions for szusergent? That would result in multiple definition linker errors, right?
    No. Each source file that #include's Variables.h will have its own local version of szuseragent.

    Personally, I wouldn't put such a declaration in a header file at all - make it local to the function that uses the string. But it it is actually needed in a header file, and the size of the string needs to be found using sizeof(), then the approach I suggested above is what is needed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain this to me (beginner w/ pointers)?
    By klawson88 in forum C Programming
    Replies: 7
    Last Post: 10-02-2010, 10:07 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. what's wrong with my pointer?
    By iluvmyafboys in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2002, 02:08 PM

Tags for this Thread