Pointer to union inside struct

This is a discussion on Pointer to union inside struct within the C Programming forums, part of the General Programming Boards category; Hi, I have the following program: Code: #include <stdio.h> typedef struct { int id; union { struct { int major; ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Pointer to union inside struct

    Hi,

    I have the following program:

    Code:
    #include <stdio.h>
    
    typedef struct  
    {
    	int id;   
    	union 
    	{
    		struct 
    		{                  
    			int major;        
    			int minor;
    		} version;
    	} data;
    } cli_struct;
    
    int main(void);
    int add(cli_struct *cli);
    
    int main(void)
    {
    	int result;
    	cli_struct cli;
    
    	cli.id = 5;
    	cli.data.version.minor = 15;
    	result = add(&cli);
    	printf("Add: %d\n", result);
    	return(0);
    }
    
    int add(cli_struct *cli)
    {
    	int result;
    
    	result = cli->id + cli->data->version->minor;
    	return(result);
    }
    When I compile with gcc I got de following message:

    error: invalid type argument of `->'

    How can I access the member "minor" inside the function "add"?
    Thanks in advance for any answer.
    Best regards,

    Luis Vital

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Your use of union is pretty pointless in this example if you ask me, but that's not a mistake per say.

    2) version is not a pointer.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Try
    Code:
    result = cli->id + cli->data->version.minor;
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    result = cli->id + cli->data.version.minor;

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To clarify, only 'cli' is the pointer, so only the first depth uses ->. None of your other accesses are via pointer, so you use the dot operator.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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