Thread: How to get allocate space for the struct player variable?

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

    How to get allocate space for the struct player variable?

    How to get allocate space for the struct player variable?

    Code:
    #include <stdio.h>
    
    struct player {
    	
        int number;
        int rank;
    };
    
    
    int main()
    {
        struct player variable = {35, 1};
    
    
        struct player *ptr;
    	
        ptr = &variable;
    	
    	printf("Meory Location of NUMBER: %p\n", &variable.number);
        printf("Memory Location of RANK: %p \n", &variable.rank);
    	
    	printf("NUMBER: %d\n", ptr->number);
        printf("RANK: %d \n",  ptr->rank);
    	
        printf("NUMBER: %d\n", (*ptr).number);
        printf("RANK: %d \n", (*ptr).rank);
    
    
        return 0;
    }
    Meory Location of NUMBER: 0061FF24
    Memory Location of RANK: 0061FF28
    NUMBER: 35
    RANK: 1
    NUMBER: 35
    RANK: 1

    Does it means 35 stored at location 0061FF24
    Does it means 1 stored at location 0061FF28

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    172
    Does it means 35 stored at location 0061FF24
    Does it means 1 stored at location 0061FF28
    Not exactly, it is not stored only at 0061FF24. 35 is an integer and integers are stored as 4 bytes of memory in this case. So, 35 is stored over 0061FF24 to 0061FF28. Similarly, 1 is stored over 0061FF28 to 0061FF31. Note that each byte is 8 bits.

    Code:
    35: 00000000 00000000 00000000 00100011
    
    0061FF24 . 1 holds 0
    0061FF24 . 2 holds 0
    0061FF24 . 3 holds 0
    0061FF24 . 4 holds 0
    0061FF24 . 5 holds 0
    0061FF24 . 6 holds 0
    0061FF24 . 7 holds 0
    0061FF24 . 8 holds 0
    0061FF24 . 9 (which is 0061FF25 . 1) holds 0
    0061FF25 . 2 holds 0
    0061FF25 . 3 holds 0
    0061FF25 . 4 holds 0
    0061FF25 . 5 holds 0
    0061FF25 . 6 holds 0
    0061FF25 . 7 holds 0
    0061FF25 . 8 holds 0
    0061FF25 . 9 (which is 0061FF26 . 1) holds 0
    0061FF26 . 2 holds 0
    0061FF26 . 3 holds 0
    0061FF26 . 4 holds 0
    0061FF26 . 5 holds 0
    0061FF26 . 6 holds 0
    0061FF26 . 7 holds 0
    0061FF26 . 8 holds 0
    0061FF26 . 9 (which is 0061FF27 . 1) holds 0
    0061FF27 . 2 holds 0
    0061FF27 . 3 holds 1
    0061FF27 . 4 holds 0
    0061FF27 . 5 holds 0
    0061FF27 . 6 holds 0
    0061FF27 . 7 holds 1
    0061FF27 . 8 holds 1
    I've represented the bits as "base address . i'th bit" which is just for explanation purpose. This is my understanding but I may be wrong.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by Zeus_ View Post
    Not exactly, it is not stored only at 0061FF24. 35 is an integer and integers are stored as 4 bytes of memory in this case. So, 35 is stored over 0061FF24 to 0061FF28. Similarly, 1 is stored over 0061FF28 to 0061FF31. Note that each byte is 8 bits.
    .
    I am trying to verify that the address of variable and pointer value is same. I have printed the memory address for variable and how to verify that address of variable and pointer are the same?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,243
    Quote Originally Posted by abhi143
    I am trying to verify that the address of variable and pointer value is same.
    They are the same by definition: the address-of operator & results in a pointer to the operand.

    If you really must, you can do the rather pointless exercise of:
    Code:
    #include <stdio.h>
     
    struct player {
         
        int number;
        int rank;
    };
     
     
    int main()
    {
        struct player variable = {35, 1};
        struct player *ptr = &variable;
        int *number_ptr = &ptr->number;
        int *rank_ptr = &ptr->rank;
        printf("Memory Location of NUMBER: %p\n", (void*)&variable.number);
        printf("Memory Location of RANK: %p\n", (void*)&variable.rank);
    
        printf("Memory Location of NUMBER by pointer: %p\n", (void*)number_ptr);
        printf("Memory Location of RANK by pointer: %p\n", (void*)rank_ptr);
         
        printf("NUMBER: %d\n", ptr->number);
        printf("RANK: %d\n",  ptr->rank);
         
        printf("NUMBER: %d\n", (*ptr).number);
        printf("RANK: %d \n", (*ptr).rank);
    
        return 0;
    }
    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

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by laserlight View Post
    They are the same by definition: the address-of operator & results in a pointer to the operand.

    If you really must, you can do the rather pointless exercise of:
    Is it possible to print memory location allocated by malloc function if I want to get memory for my program how to know allocated space

    Code:
    #include <stdio.h> #include <stdlib.h>
     
    struct player
    {
       int number;
       float rank;
    };
     
    int main(void)
    {
       struct player *Ptr = malloc(8 * sizeof(struct player));
    	
        if (Ptr != NULL)
        {
            Ptr->number = 35;
            Ptr->rank = 1;
             
            printf("Number: %d\n", Ptr->number);
            printf("rank: %f\n",   Ptr->rank);
             
            free(Ptr);
            Ptr = NULL;
        }
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,243
    Stop bothering with printing addresses/pointers. If you want to know if space has been allocated by malloc, check that the return value of malloc is not a null pointer. That's all that you need to do; there may be special cases in which the underlying implementation of malloc for an OS is such that malloc may not necessarily return a null pointer even when memory could not be allocated, but those are beyond your control.

    Printing of addresses is usually only a debugging tool, and so you could use a debugger if you must.

    But yes, just stop. Your obsession with printing memory locations is unhealthy from the perspective of programming in C. Stop. Just stop. Resist the urge to print addresses. Resist the urge to create superfluous pointers just so you can print them.

    Rather, cultivate the habit of checking that pointers that could be null are not null before using them. This means checking the return value of malloc and other functions that return pointers that could be null, and checking pointer parameters.
    Last edited by laserlight; 2 Weeks Ago at 02:28 AM.
    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

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by laserlight View Post
    Stop bothering with printing addresses/pointers..
    I have misunderstanding on pointer of structure. Pointer store address so I think pointer of structure store address of structure variable

    number and rank are the data member of structure and Ptr is structure variable so Ptr will store address of number and rank and we can store value at the address pointed by pointer

    I am really confused I don't understand how to remember what's useful

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,243
    Well okay. If you want to clarify what are the addresses of a struct object versus its data members, then yes, that's a good time to print addresses. But it really doesn't have to be so hard:
    Code:
    #include <stdio.h>
    
    struct player
    {
        int number;
        float rank;
    };
    
    int main(void)
    {
        struct player x = {35, 1};
        printf("address of x=%p\naddress of x.number=%p\naddress of x.rank=%p\n",
               (void*)&x, (void*)&x.number, (void*)&x.rank);
        return 0;
    }
    Now, examine the output. What do you get? What does it mean?
    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

  9. #9
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by laserlight View Post
    Well okay. If you want to clarify what are the addresses of a struct object versus its data members, then yes, that's a good time to print addresses. But it really doesn't have to be so hard:
    How to see the address of data member in code

    Code:
    #include <stdio.h> #include <stdlib.h>
      
    struct player
    {
       int number;
       float rank;
    };
      
    int main(void)
    {
       struct player *Ptr = malloc(8 * sizeof(struct player));
         
        if (Ptr != NULL)
        {
            Ptr->number = 35;
            Ptr->rank = 1;
              
            printf("Number: %d\n", Ptr->number);
            printf("rank: %f\n",   Ptr->rank);
    		     			
    		printf("address of structure variable : %p \n", (void*)Ptr);
              
            free(Ptr);
            Ptr = NULL;
        }
        return 0;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,243
    Quote Originally Posted by abhi143
    How to see the address of data member in code
    Refer to my previous post.
    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

  11. #11
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by laserlight View Post
    Refer to my previous post.
    okay I have done but not sure

    Code:
    #include <stdio.h> #include <stdlib.h>
       
    struct player
    {
       int number;
       int rank;
       
       struct player* object;
    };
       
    int main(void)
    {
       struct player *Ptr = malloc(8 * sizeof(struct player));
          
        if (Ptr != NULL)
        {
            Ptr->number = 35;
            Ptr->rank = 1;
               
            printf("address of Ptr : %p \n", (void*)Ptr);
    		
    	    printf("address of Ptr->number=%p \n", (void*)Ptr->number);
    		printf("address of Ptr->rank=%p \n",  (void*)Ptr->rank);
    		
            printf("Number: %d\n", Ptr->number);
            printf("rank: %d\n",   Ptr->rank);
                             	            
            free(Ptr);
            Ptr = NULL;
        }
        return 0;
    }
    address of Ptr : 00722610
    address of Ptr->number=00000023
    address of Ptr->rank=00000001
    Number: 35
    rank: 1

    I don't understand how to get the address of third data member that is struct player* object?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,243
    The address of Ptr->number is &Ptr->number, not Ptr->number.

    But why do you want to know its address? What's the point? I showed you a simple example in post #8 and asked you two related questions to get you to think about it, but you completely ignored me!

    Quote Originally Posted by abhi143
    I don't understand how to get the address of third data member that is struct player* object?
    So you don't know how to even get that data member? Huh? Are you trolling or what?
    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

  13. #13
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by laserlight View Post
    The address of Ptr->number is &Ptr->number, not Ptr->number.
    I have corrected it
    Code:
      printf("address of Ptr->number=%p \n", (void*)&Ptr->number);
            printf("address of Ptr->rank=%p \n",  (void*)&Ptr->rank);
    Quote Originally Posted by laserlight View Post
    But why do you want to know its address? What's the point? I showed you a simple example in post #8 and asked you two related questions to get you to think about it, but you completely ignored me!
    I just wanted to know allocated space and data stored into that space. this is the reason I asked you my doubts

    Quote Originally Posted by laserlight View Post
    So you don't know how to even get that data member? Huh? Are you trolling or what?
    No because third data member is different then other data members and I searched on google how to get address to pointer of structure I couldn't get it

    Code:
    struct player{
       int number;
       int rank;
        
       struct player* object;
    };

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,532
    What?
    You mean you never actually tried this?
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
        
    struct player
    {
       int number;
       int rank;
        
       struct player* object;
    };
        
    int main(void)
    {
       struct player *Ptr = malloc(8 * sizeof(struct player));
           
        if (Ptr != NULL)
        {
            Ptr->number = 35;
            Ptr->rank = 1;
            Ptr->object = Ptr+1;    //!! For funsies
                
            printf("address of Ptr : %p \n", (void*)Ptr);
             
            printf("address of Ptr->number=%p \n", (void*)&Ptr->number);   //!! you forgot these &
            printf("address of Ptr->rank  =%p \n", (void*)&Ptr->rank);
            printf("address of Ptr->object=%p \n", (void*)&Ptr->object);   //!!, No? Really!
    
            printf("Number: %d\n", Ptr->number);
            printf("rank: %d\n",   Ptr->rank);
            printf("object: %p\n",  (void*) Ptr->object);
                                             
            free(Ptr);
            Ptr = NULL;
        }
        return 0;
    }
    > No because third data member is different then other data members
    How is it different?
    Look closely, it's not so different that say trying the obvious thing first wouldn't work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 39
    Last Post: 07-08-2013, 09:59 AM
  2. Replies: 2
    Last Post: 12-11-2012, 03:19 PM
  3. allocate space
    By Chris_1980 in forum C Programming
    Replies: 5
    Last Post: 06-10-2010, 07:28 AM
  4. Allocate space for 2d array
    By jtay in forum C Programming
    Replies: 7
    Last Post: 04-25-2010, 10:34 PM
  5. allocate no space for dynamic array~
    By black in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2004, 06:28 AM

Tags for this Thread