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

1. ## 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. 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. Originally Posted by Zeus_
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. 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;
}

5. Originally Posted by laserlight
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. 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.

7. Originally Posted by laserlight
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. 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};
(void*)&x, (void*)&x.number, (void*)&x.rank);
return 0;
}
Now, examine the output. What do you get? What does it mean?

9. Originally Posted by laserlight
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. Originally Posted by abhi143
How to see the address of data member in code
Refer to my previous post.

11. Originally Posted by laserlight
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("Number: %d\n", Ptr->number);
printf("rank: %d\n",   Ptr->rank);

free(Ptr);
Ptr = NULL;
}
return 0;
}
Number: 35
rank: 1

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

12. 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!

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?

13. Originally Posted by laserlight
The address of Ptr->number is &Ptr->number, not Ptr->number.
I have corrected it
Code:
Originally Posted by laserlight
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

Originally Posted by laserlight
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. 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.