I know about pointer of variables.
int x = 10;
int *ptr = &x;
In these expression three things involved:
x
takes 2 byte of memory because it is an integer.
ptr
takes 2 bytes also.
If memory address of x
is 1004, then ptr
will stores 1004.
In these example whenever we use *ptr
in program, It give us a value which stored at memory addrs - 1004
But what happens in structure? Let take example:
struct book {
int a;
int b;
}
struct book str1, str2;
struct book *ptr = &str1;
I have three question about this example:
How does ptr
holds complete address of str1
?
What is the difference between pointer to variable and pointer to complete structure in memory?
How many bytes ptr
will take?
Do ptr
have members? If it have members so what type of members it will have? (Pointer or variable)
and one more thing- what does with object of classes instead of it
1) how does ptr holds complete addrs of str1?
It holds the starting address. It doesn't need to hold a range or anything.
2) how would be architecture of ptr in memory?
Internally it's a 32-bit or 64-bit unsigned integer depending on the operating system bitwidth (interpreted as a pointer).
3) how many bytes ptr will take?
4 or 8 bytes (for a 32- or 64-bit OS).
4) do ptr have members? If it have members so what type of members it will have? (Pointer or variable)
You can dereference the pointer using the ->
operator as in
int x = ptr->a;