Thread: deep confuse with pointer

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    23

    deep confuse with pointer

    Hi,

    I am deeply confuse with pointer after so many reading

    Pointer is variable that hold memory location of variable. variable may be a different type like int, char, float..

    I am confused with pointer to structure.

    Code:
    #include<stdio.h>
    
    struct structure_name
    {
        int i;
        char c;
        float f;
        struct structure_name * n;
    }
    
    
    int main ()
    {
        struct structure_name * structure_variable = NULL;
        
         structure_variable = malloc (sizeof(structure_variable));
    
         structure_variable->i;
         structure_variable->c;
         structure_variable->f;
    
    
        return 0;    
    }

    Does pointer store the memory location of structure or location of structure member?

    structure_variable->i; Pointer hold the memory location of structure member i variable i is integer variable
    Last edited by Parth12; 02-18-2020 at 11:43 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Quote Originally Posted by Parth12
    Does pointer store the memory location of structure or location of structure member?
    It stores the address of the structure object.

    This address is the same in value as one of the members of the struct object (i.e., the first) because the struct object consists of these members, but it is different in type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by laserlight View Post
    It stores the address of the structure object.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
     
    struct structure_name
    {
        int i;
        char c;
        float f;
        struct structure_name * n;
    };
     
     
    int main ()
    {
        struct structure_name * structure_variable = NULL;
         
         structure_variable = malloc (sizeof(structure_variable));
     
         structure_variable->i;
         structure_variable->c;
         structure_variable->f;
         
         printf("pointer of structure : %p \n", structure_variable);
         
         printf("memory location of first object of structure : %p \n", &(structure_variable->i ));
         
         printf("memory location of second object of structure : %p \n", &(structure_variable->c ));
          
          printf("memory location of third object of structure : %p \n", &(structure_variable->f ));
     
     
        return 0;    
    }
    pointer of structure : 007113A8
    memory location of first object of structure : 007113A8
    memory location of second object of structure : 007113AC
    memory location of third object of structure : 007113B0

  4. #4
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    something better then previous

    Code:
    #include<stdio.h>
    #include<stdlib.h>
     
    struct structure_name
    {
        int i;
        char c;
        float f;
        struct structure_name * n;
    };
     
     
    int main ()
    {
        struct structure_name * structure_variable = NULL;
         
         structure_variable = malloc (sizeof(structure_variable));
     
         structure_variable->i = 100;
         structure_variable->c = 'x';
         structure_variable->f = 23.30;
         
         printf("pointer of structure : %p \n", structure_variable);
         
         printf("memory location of first object of structure : %p \n", &(structure_variable->i ));
         
         printf("memory location of second object of structure : %p \n", &(structure_variable->c ));
          
         printf("memory location of third object of structure : %p \n", &(structure_variable->f ));
     
         
         printf("pointer point to the loaction  %p of first object of structure where  i is : %d \n", &(structure_variable->i ), (structure_variable->i ));
         
         printf("pointer point to the loaction  %p of second object of structure where  c  is : %c \n", &(structure_variable->c ), (structure_variable->c ));
         
         printf("pointer point to the loaction  %p of third object of structure where  f is : %f \n", &(structure_variable->f ), (structure_variable->f ));
          
        return 0;    
    }
    pointer of structure : 00B413A8


    memory location of first object of structure : 00B413A8

    memory location of second object of structure : 00B413AC

    memory location of third object of structure : 00B413B0

    pointer point to the loaction 00B413A8 of first object of structure where i is : 100

    pointer point to the loaction 00B413AC of second object of structure where c is : x

    pointer point to the loaction 00B413B0 of third object of structure where f is : 23.299999

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    714
    By the way, this line:

    Code:
    structure_variable = malloc (sizeof(structure_variable));
    should be:

    Code:
    structure_variable = malloc (sizeof(*structure_variable));
    because you want to allocate enough memory to hold an object that structure_variable points to (a struct structure_name), not just enough memory to hold a pointer to a struct structure_name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions confuse me so, what am i doing wrong?
    By Ctylersills in forum C Programming
    Replies: 11
    Last Post: 04-20-2019, 04:22 AM
  2. I'm new to C, confuse with something..
    By Milo Juak MuTou in forum C Programming
    Replies: 5
    Last Post: 05-14-2012, 09:38 AM
  3. PayPal confuse me
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-20-2009, 11:56 AM
  4. a little confuse o_0???
    By omarbags in forum C++ Programming
    Replies: 15
    Last Post: 01-24-2008, 01:03 AM
  5. Too confuse
    By dv007 in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2002, 05:33 PM

Tags for this Thread