Thread: error: request for member 'struct variable' in something not a str

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    error: request for member 'struct variable' in something not a str

    I want to assign msg.modification_time, but with following codebit I only get "error: request for member 'modification_time' in something not a structure or union". I can't figure out why! Can someone please guide me towards the solution?

    Code:
    struct meta {
      char modification_time[10];
    };
    void something() {
      struct meta *msg = malloc(sizeof(struct meta));
      struct timeval tv;
      time_t tidNaa;
      gettimeofday(&tv, NULL);
      tidNaa = tv.tv_sec;
      strftime(msg.modification_time,10,"%T",localtime(&tidNaa));
    }
    Btw: I also tried to declare a char timenow[10] in main, and use strftime(timenow,10,"%T",localtime(&tidNaa));
    and that worked. So it is propably the struct that is causing the troubles. What's wrong?
    Last edited by duckdace; 10-16-2010 at 05:29 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    msg is a pointer; thus you must write
    msg->modification_time
    But, that is not necessary either.
    You can change
    struct meta *msg = malloc(sizeof(struct meta));
    to
    struct meta msg;
    And all will work fine. Otherwise you have to free, which you don't.

    Also, Stroustrup: C++ Style and Technique FAQ
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    Quote Originally Posted by Elysia View Post
    msg is a pointer; thus you must write
    msg->modification_time
    But, that is not necessary either.
    You can change
    struct meta *msg = malloc(sizeof(struct meta));
    to
    struct meta msg;
    And all will work fine. Otherwise you have to free, which you don't.

    Also, Stroustrup: C++ Style and Technique FAQ
    Omg, I can't believe I wrote void main. The thing is that it's not supposed to be main, but I had my mind elsewhere when i posted the thread.

    I thought -> was restricted to when modification_time was a pointer, so your post was very helpful.
    Actually I do have to allocate memory, but not in this part of the code. Thanks.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    msg->something is a shorthand for (*msg).something.
    Because "." has lower precedence than "*", *msg.something would essentially become *(msg.something). But msg is a pointer, and thus does not have any members. But what it points to have members; thus you need to dereference it first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  2. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  3. A struct member as a pointer to a variable
    By mc61 in forum C Programming
    Replies: 5
    Last Post: 01-27-2008, 08:40 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM