Thread: pointer to structure in c

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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