Pointer to Structure not working

This is a discussion on Pointer to Structure not working within the C Programming forums, part of the General Programming Boards category; Hi, In the following program, the structure "Root" is defined globally. Pointer to this structure gives compilation error. Code: #include<stdio.h> ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    18

    Pointer to Structure not working

    Hi,

    In the following program, the structure "Root" is defined globally. Pointer to this structure gives compilation error.


    Code:
    #include<stdio.h>
    
    typedef struct
    {
    int a;
    float f;
    }Root;
    
    
    int main()
    {
            Root *root, root_h;
    
            root_h.a=1;
            root_h.f=10.11;
    
            root = &root_h;
    
            printf("\n Values : \n root.a = %d \n root.f = %f \n", root_h->a, root_h->f);
    
            return 0;
    }
    The compilation error is:

    error: invalid type argument of ‘->''

    How to resolve this error?

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    63
    Try with this one.
    printf("\n Values : \n root.a = %d \n root.f = %f \n", root->a, root->f);

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    18
    Ok. Thanks.

    If the program is using a pointer to strutcure, why it gives error if value is assigned directly to struct elements, as follows?

    Code:
    #include<stdio.h>
    
    typedef struct 
    {
    int a[2];
    float f;
    }Root;
    
    int main()
    {
    	Root *root;
    
    	root.a[0]=0;
    	root.a[1]=1;
    	root.f=10.11;
    
    	printf("\n Values : \n root.a[0] = %d \n root.a[1] = %d \n root.f = %f \n", root.a[0], root.a[1], root.f);
    
    	return 0;
    }
    The compilation error is:

    # gcc -g ker_data.c -o ker_data
    ker_data.c: In function ‘main’:
    ker_data.c:13: error: request for member ‘a’ in something not a structure or union
    ker_data.c:14: error: request for member ‘a’ in something not a structure or union
    ker_data.c:15: error: request for member ‘f’ in something not a structure or union
    ker_data.c:17: error: request for member ‘a’ in something not a structure or union
    ker_data.c:17: error: request for member ‘a’ in something not a structure or union
    ker_data.c:17: error: request for member ‘f’ in something not a structure or union

    Why these errors? Because, if:

    int *a;

    we can use: a = 10;

    But why not the same with struct?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    63
    Dont mess up with Object of a structure and pointer to a structure.
    When you declare pointer to structure, it doesn't contain memory for the structure variables.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    If root is a pointer to a struct you must use -> to access the struct members and not '.' But, that pointer root must also first be pointing to valid memory. In your post #3, root will contain a random (invalid) address. You cannot simply start accessing its data members - even if you were using -> instead of '.' - unless you first point it somewhere that makes sense.

    Why these errors? Because, if:

    int *a;

    we can use: a = 10;

    But why not the same with struct?
    That's not doing what you probably think it is. "a" is a pointer and as such represents an address, a memory location. Assigning 10 to "a" simply means that you are saying "a" points to memory address 10. It does not mean that what "a" points to contains 10 but rather where "a" points to. This is fine, you can do it all day long. The problem would come when you try to access or write to what "a" points to because unless that address you assigned to "a" is a good one you are accessing memory that doesn't belong to you and you'd likely cause the program to crash.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 10:40 PM
  2. Referencing pointer inside a structure pointer
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 11-11-2010, 10:33 AM
  3. Repetition structure not working?!?
    By jusfry01 in forum C Programming
    Replies: 8
    Last Post: 06-02-2010, 06:58 PM
  4. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  5. Switch structure not working
    By him61 in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2007, 05:35 PM

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