Thread: Dereferencing Structures

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    24

    Dereferencing Structures

    Code:
    typedef struct abc
    	{
    		int y;
    		char *b;
    	}abc, *abcptr;
    abc x={2,"sdgare"};
    void *a;
    
    
    void function(void *data)
    {
    	a=data;
    }
    
    
    int main()
    {
    	function(&x);
    	printf("%d",(abcptr)(a)->y);	
    	return 0;
    }
    This code gives me an error:- error C2223: left of '->y' must point to struct/union

    Could someone please tell me why and how to make it work?
    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Your parentheses are wrong, it should be:
    Code:
    ((abcptr)a)->y
    Also, you should avoid using globals and using typedefs to disguise pointers needlessly.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    24
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structures: dereferencing pointer to incomplete type
    By Adam Rinkleff in forum C Programming
    Replies: 6
    Last Post: 07-26-2011, 02:35 PM
  2. Dereferencing
    By kkk in forum C Programming
    Replies: 1
    Last Post: 06-01-2011, 01:14 PM
  3. Structures, Pointer Elements, and Dereferencing
    By cyreon in forum C Programming
    Replies: 3
    Last Post: 05-22-2009, 11:05 AM
  4. Dereferencing
    By Air in forum C Programming
    Replies: 7
    Last Post: 05-13-2009, 08:53 AM
  5. Dereferencing Pointers help
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2007, 06:43 PM