Thread: Pointers to structures

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Question Pointers to structures

    Dear All,

    I've inherited a real-time test rig control system without any documentation and despite reading a couple of books and the articles on this site, I'm still getting confused by pointers to structures.

    I have highlighted three problems, which I've tried to reduce to the simplest case. Any help that can be offered would be most appreciated.

    The struct is defined and named outside the main function in the line,

    Code:
    typedef struct {int command; int intValue; double floatValue;} GuiMessage;
    Problem 1: This is followed immediately by the below function. (cmd has not previously been declared.) Can you declare a previously unknown pointer when declaring the arguments of a function? Also, does this argument constitute a definition of a pointer to a struct of type GuiMessage?

    Code:
    void send_command(GuiMessage *cmd)
    {
    	rtf_put(cmd, sizeof(GuiMessage));
    }
    Problem 2: Now within the main function, the line

    Code:
    GuiMessage cmd, *buf;
    seems to declare a struct, not a pointer to a struct, called cmd AND a pointer to a struct of type GuiMessage called buf. Are you allowed to give the same name to both a struct and a pointer to a struct because of 'name space'?

    Problem 3: The following line lies within a function called by main.
    What does the following sytax do? Am I right in thinking that this means that all of the values in the struct pointer to by cmd are set to 0?

    Code:
    GuiMessage cmd={0};
    Any help would be greatly appreciated.

    Thanks,

    TJHC

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Whether cmd is previously declared is irrelevant. ALL parameters to functions get fresh names when used in a parameter list. That is, even if cmd was defined elsewhere, this cmd would be completely and totally different.
    2. Why do you think "cmd" and "buf" are the same name? They don't even have a letter in common.
    3. It declares a variable of type GuiMessage called cmd and defines it to be 0 (which means all the members inside the struct are 0.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Hi tabstop,

    Thanks for the speedy reply!

    I'm satisfied with your replies to points 1 and 3...

    But, just to clarify, in Prob 1, 'cmd' is declared as a pointer. However, in Prob 2, with this pointer called 'cmd' already in existence, it appears that a struct called 'cmd' is then declared (at the same time as a pointer called buf). My confusion there is that there appears to be both a pointer and struct with the same name.

    Thanks,

    TJHC

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    2. There is no pointer called cmd already in existence. The names in parameter lists do NOT exist outside their functions.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Ugh.

    I'm new. Can you tell?

    Thanks!

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    2. If you declare this
    Code:
    GuiMessage a, b, c, *d;
    you will have three variables (a,b,c) that there type is GuiMessage and a variable (d) that its type is a pointer to a GuiMessage structure. You are probably confusing the name of the variable with the type of the variable. In any case in simple words, you will have three structures and a pointer to a structure.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    typedef struct {int command; int intValue; double floatValue;} GuiMessage;
    does this mean the same as:

    Code:
    struct GuiMessage {int command; int intValue; double floatValue;} ;
    or
    Code:
    struct {int command; int intValue; double floatValue;} GuiMessage;
    in which case the struct prototype is unnamed? I presume the former.
    nb. this is the first time i've ever typed: typedef

    What it might be important for the perhaps really new shininghelmet to remember about point 1 is this:
    The variable names in a function argument list are local to the function and not necessarily the same as the name of the variables sent to the function. So in your example, send_command() could have been called with:
    Code:
    send_command(buf);
    presuming buf is a pointer to a GuiMessage (GuiMessage must already be declared). Within the function definition itself, buf (or whatever) is known as cmd. However, IN MEMORY they both point to the same place, so what happens to cmd will happen now to buf.
    Last edited by MK27; 09-10-2008 at 10:57 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Code:
    typedef struct {int command; int intValue; double floatValue;} GuiMessage;
    does this mean the same as:

    Code:
    struct GuiMessage {int command; int intValue; double floatValue;} ;
    or
    Code:
    struct {int command; int intValue; double floatValue;} GuiMessage;
    in which case the struct prototype is unnamed? I presume the former.
    nb. this is the first time i've ever typed: typedef
    Or perhaps C: None of the above. It creates a new (unnamed) type, that is a struct with two ints and a double -- this is the "struct etc" part. The typedef then says this (unnamed) type shall be henceforward known as GuiMessage.

    Your first creates a new type called "struct GuiMessage". Your second creates a new (unnamed) type and then declares a variable of that new (unnamed) type called GuiMessage.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    So what is the difference between a new (unnamed) type, that is a struct with two ints and a double...[that] shall be henceforward known as GuiMessage. and a new type called "struct GuiMessage"?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Except for the actual definition, it changes how you use it.
    The first can simply be used as

    GuiMessage msg;

    The second must be used as

    struct GuiMessage msg;

    Due to C rules (this, however, does not apply to C++, so in C++ they would actually both be the same, in case you're wondering).
    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.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    (this, however, does not apply to C++, so in C++ they would actually both be the same, in case you're wondering).
    Even in C++ there is a difference: in the first case the following is not allowed, where as in the second case both syntax variants can be used.

    struct GuiMessage msg;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Another pointer question

    Earlier (top of thread) we saw the pointer,

    Code:
    GuiMessage *cmd
    I understand that this differs from

    1.
    Code:
    GuiMessage * cmd
    2.
    Code:
    GuiMessage* cmd
    and
    3.
    Code:
    GuiMessage** cmd
    But how?!

    I've been searching for articles on pointer syntax but keep finding reasonably simple articles that don't go as far as to explain these...

    Thanks.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    T *p
    T * p
    T* p
    ...are all the same thing. It's just a matter of syntactic sugar.
    Some reason that T* means that the pointer is a type (T*),
    T *p reasons that *p is T and
    T * p is the middle ground (neither T* nor *p).

    The last is a pointer to pointer (ie a pointer to type T*).
    I like T* myself.
    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.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The first 2 are identical, it's a long winded debate -- does the pointer belong to the name or type?

    You'll see
    Code:
    GuiMessage* cmd;
    And
    Code:
    GuiMessage *cmd;
    And
    Code:
    GuiMessage * cmd;
    Which all mean the same thing in this case -- be careful, they don't mean the same thing when you're using not declaring, reply if you want an example of this.

    The 3rd one (your 3.) is a pointer to a pointer of type GuiMessage.
    Last edited by zacs7; 09-11-2008 at 04:33 AM.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Go on...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  3. returning pointers to structures
    By Giant in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2005, 08:40 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM