Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) (→{{Template:Author|Arjun Suresh|{{arjunweb}} }}) |
||
Line 26: | Line 26: | ||
==={{Template:Author|Arjun Suresh|{{arjunweb}} }}=== | ==={{Template:Author|Arjun Suresh|{{arjunweb}} }}=== | ||
− | Assume start location of arr | + | Assume start location of arr is 1000. |
So, the first two elements of the array will be stored in memory as follows: | So, the first two elements of the array will be stored in memory as follows: | ||
− | 1000: 0 0 0 0 0 0 0 2 (hexa decimal representation and assuming int takes 4 bytes) | + | 1000: 0 0 0 0 0 0 0 2 (hexa decimal representation and assuming int takes 4 bytes and assuming least significant byte is stored at highest address(little-endian)) |
1004: 0 0 0 0 0 0 0 3 | 1004: 0 0 0 0 0 0 0 3 | ||
<syntaxhighlight lang="c" name="pointer_1"> int main() {
int arr[3] = {2, 3, 4}; char *p; p = (char*)arr; printf("%d ", *p); p = p+1; printf("%d\n", *p); return 0;
}
</syntaxhighlight>
(A) 2 3
(B) 2 0
(C) 1 0
(D) Garbage value
Assume start location of arr is 1000. So, the first two elements of the array will be stored in memory as follows:
1000: 0 0 0 0 0 0 0 2 (hexa decimal representation and assuming int takes 4 bytes and assuming least significant byte is stored at highest address(little-endian)) 1004: 0 0 0 0 0 0 0 3
Now, %d, *p will print the first 4 bytes starting at 1000, that is 2. When p is incremented, it will go to 1001, as p is a char pointer.
1001: 0 0 0 0 0 0 0 0
So, output will be 0.
<syntaxhighlight lang="c" name="pointer_1"> int main() {
int arr[3] = {2, 3, 4}; char *p; p = (char*)arr; printf("%d ", *p); p = p+1; printf("%d\n", *p); return 0;
}
</syntaxhighlight>
(A) 2 3
(B) 2 0
(C) 1 0
(D) Garbage value
Assume start location of arr is 1000. So, the first two elements of the array will be stored in memory as follows:
1000: 0 0 0 0 0 0 0 2 (hexa decimal representation and assuming int takes 4 bytes and assuming least significant byte is stored at highest address(little-endian)) 1004: 0 0 0 0 0 0 0 3
Now, %d, *p will print the first 4 bytes starting at 1000, that is 2. When p is incremented, it will go to 1001, as p is a char pointer.
1001: 0 0 0 0 0 0 0 0
So, output will be 0.