Thread: pointer to structure in c

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    40

    pointer to structure in c

    I have written code to understand pointer to structure

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct point    //define structure 
    {
       int x;      //structure member type integer 
       char y;     // structure member type charactr 
    };
    
    
    void main ()   
    {
       struct point *p = NULL;  // p is pointer to structure variable and the value stored in pointer is NULL
       
       p = malloc(sizeof(*p));  // Allocate dyanamic memory for pointer to structure variable p
       
       p->x = 8;   // p is pointer to structure pointing to variable x that is integer type and value store in x is 8 
       
       p->y = 'A'; // p is pointer to structure pointing to variable y that is char type and value store in y is 'A'
    
    
    }

    I have doubt, do my last two comments match with the code lines ?

    if not so what should i write in comments

    Code:
       p->x = 8;   // p is pointer to structure pointing to variable x that is integer type and value stored in x is 8 
       
    p->y = 'A'; // p is pointer to structure pointing to variable y that is char type and value stored in y is 'A'
    If not what should i write in comments
    Last edited by skyr6546; 01-03-2020 at 05:19 AM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Why would the type of p change?

    Code:
    struct point    //define structure
    {
       int x;      //structure member type integer
       char y;     // structure member type charactr
    };
     
     
    int main (void)  
    {
        struct point *p = NULL;     // p points to an object of type struct point but currently points to nothing
    
        p = malloc(sizeof(*p));     // Allocate dynamic memory for a variable of type struct point. If malloc() succeeds it will point to an object of type struct point
                                    // The type of p has not changed (it's still a pointer to an object of struct point; i.e. it's a pointer to a "variable" that has the type struct point... assuming that malloc() didn't fail and p is not NULL)
    
        p->x = 8;                   // p is a pointer to an object of struct point. The type of p->x is int. Store the value 8 in it.    
        p->y = 'A';                 // p is a pointer to an object of struct point. The type of p->y is char. Store the value 'A' in it.
       
    }
    Edit: I'm not really sure why you think that the type of what p points to can change... under normal circumstances (and in this example) it doesn't (you can change it, but this example doesn't and normally you probably wouldn't; you'd need to use an explicit cast if for some reason you needed to)

    I've made the type of p purple
    Last edited by Hodor; 01-03-2020 at 05:46 AM.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    40
    Quote Originally Posted by Hodor View Post
    Why would the type of p change?
    Edit: I'm not really sure why you think that the type of what p points to can change... under normal circumstances (and in this example) it doesn't (you can change it, but this example doesn't and normally
    I've made the type of p purple
    my another doubts

    p->x is initialized to 8 or only x is initialized to 8

    Code:
    p->x ;     // p is a pointer to an object of struct point. it point to the object of structure x  but p->x is not initialized 
    
    p->x = 8;    // p is a pointer to an object of struct point. it point to the object of structure x and  p->x is  initialized to 8 or only  x is initialized to 8//
    
    Last edited by skyr6546; 01-03-2020 at 06:01 AM.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Only p is a pointer

    p->x is the same as (*p).x

    The only pointer is p. Assuming p is not NULL it points to a structure of type struct point.

    Code:
    struct point    //define structure
    {
       int x;      //structure member type integer
       char y;     // structure member type charactr
    };
    
    int main(void)
    {
       struct point my_struct;
    
       my_struct.x = 8; // my_struct.x is not a structure. x is a member of the struct (type int)
    
       // Similarly
       struct point *p = &my_struct; // p is a pointer to an object of type struct point
       p->x = 8;    // p is a pointer to an object of type struct point. x is a member of the object being pointed to by p (x has type int, as above)
       (*p).x = 8;  // this is the same as the previous line
    
       return 0;
    } 
    

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    I guess I've forgotten how to explain pointers. I don't think about the intermediate steps anymore (maybe never have because I experienced indirection long before I learned C) and without writing a huge wall of text I can't explain it. And I don't want to write a wall of text... maybe someone else can.

    But, consider
    Code:
    struct point    //define structure
    {
       int x;      //structure member type integer
       char y;     // structure member type charactr
    };
    
    
    int main(void)
    {
       struct point my_struct;  // What type is 'point'?
       struct point *p; // What type is p?
    
       my_struct.x; // What type is x? (it's not a pointer)
       my_struct.y; // What type is y? (it's not a pointer)
    
       (*p).x; // What type is x? (it's not a pointer)
    
       return 0;
    }
    
    p->x is just shorthand for (*p).x

  6. #6
    Registered User
    Join Date
    Nov 2019
    Posts
    40
    Quote Originally Posted by Hodor View Post
    I guess I've forgotten how to explain pointers. I don't think about the intermediate steps anymore (maybe never have because I experienced indirection long before I learned C) and without writing a huge wall of text I can't explain it. And I don't want to write a wall of text... maybe someone else can.

    But, consider
    I didn't understand
    Code:
    p->x = 8;
    I understand p is pointer and it pointing to the object x of structure. p hold the memory location of x and value stored in x is 8

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by skyr6546 View Post
    I didn't understand
    Code:
    p->x = 8;
    I understand p is pointer and it pointing to the object x of structure. p hold the memory location of x and value stored in x is 8
    p holds the "memory location" of the structure, not member x of the structure belonging to the structure being pointed to

    (better to think of p as pointing to a structure than being a memory location, but.. yeah)

  8. #8
    Registered User
    Join Date
    Nov 2019
    Posts
    40
    Quote Originally Posted by Hodor View Post
    p holds the "memory location" of the structure, not member x of the structure belonging to the structure being pointed to

    (better to think of p as pointing to a structure than being a memory location, but.. yeah)
    Output of program

    address of p : 00000000
    address of p : 007113A8
    address of p->x : 007113A8
    value of p->x : A
    value of p->x : B
    address of p : 007113A8
    address of p->x : 007113A8
    address of p : 007113A8

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct point    //define structure 
    {
       char x;      //structure member type integer 
    };
    
    
    void main ()   
    {
       struct point *p = NULL;  
    
    
       printf("address of p : %p \n", (void*)p);
       
       p = malloc(sizeof(*p));  
       printf("address of p : %p \n", (void*)p);  
       printf("address of p->x : %p \n", (void*)&p->x);
      
       p->x = 'A';     
       printf("value of p->x : %c \n", p->x); 
       
       p->x = 'B';     
       printf("value of p->x : %c \n", p->x); 
       
       printf("address of p : %p \n", (void*)p);  
       printf("address of p->x : %p \n", (void*)&p->x);
       
       free(p);
       printf("address of p : %p \n", (void*)p);
    }

    first time assigning 'A'
    Memory location = 007113A8 = A

    second time assigning 'B'
    Memory location = 007113A8 = B

    only value will change but address of pointer will not change

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-04-2017, 07:42 AM
  2. Replies: 2
    Last Post: 04-26-2011, 10:40 PM
  3. Referencing pointer inside a structure pointer
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 11-11-2010, 11:33 AM
  4. Replies: 9
    Last Post: 06-13-2009, 02:31 AM

Tags for this Thread