Calling struct member error

This is a discussion on Calling struct member error within the C Programming forums, part of the General Programming Boards category; Code: typedef struct call_info_t { char id[20]; } *call_info; void cs_logging(char *logstr) { char logline[50]; snprintf(logline, sizeof logline, "%s%s%s, ":id:", ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    22

    Calling struct member error

    Code:
    typedef struct call_info_t
    {
        char id[20];
    
    } *call_info;
    
    
    void cs_logging(char *logstr)
    {
        char logline[50];
        snprintf(logline, sizeof logline, "%s%s%s, ":id:", call_info.id, logstr);
    
        FILE *file;
        file = fopen("output_trace.log","a+");
        fprintf(file, "%s\n", logline);
        fclose(file);
    }
    Why am I getting this error
    error: expected expression before 'call_info'

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,322
    Look at the syntax highlighting on the code you posted:
    Code:
    snprintf(logline, sizeof logline, "%s%s%s, ":id:", call_info.id, logstr);
    Why does the blue string highlighting stop before :id:? Because you have an extra " in the middle of your string. If you want it there, you have to escape it with \". You should be using a editor that has decent syntax highlighting. It's immensely helpful.

    Also, you declared call_info to be a pointer to struct call_info_t, so you need to use -> to access the members, not the dot operator.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    22

    Angry

    Code:
    snprintf(logline, sizeof logline, "%s%s%s", ":id:", call_info->id, logstr);
    sorry, I missed up in pasting the code it should be "%s%s%s".

    I have now tried with -> operator but it is still giving the same error message. Would it have anything to do with string formatting?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,322
    Doh! I misread the struct definition. You are actually defining a type, there (see the keyword 'typedef'?), not a variable. You created a new type, called call_info, that is a pointer to a struct call_info_t type. The -> operator expects a variable on the left side. You need to declare a variable of the appropriate type, and use it's name instead of call_info, e.g. variable_name->id or variable_name.id.

    Note, most people frown on typedef'ing a pointer unless you're making a totally opaque data type, but I don't think that's the case here. Typedef'ing pointers just makes it harder to keep track of what's going on, how much dereferencing you should do, etc. It makes pointer bugs much more likely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-16-2010, 06:27 AM
  2. bus error when assigning value to member of struct
    By popapez in forum C Programming
    Replies: 4
    Last Post: 09-23-2009, 08:37 PM
  3. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  4. struct member parse error
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 05-08-2006, 08:50 PM
  5. Replies: 0
    Last Post: 11-29-2002, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21