C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 13)
13.
What will be the output of the program?
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1);
printf("%d", *p);
return 0;
}
Discussion:
87 comments Page 3 of 9.
Bunny said:
9 years ago
Those who are getting an error.
In some compiler, you can equate two different data type pointers so you have to typecast them.
Let's say the base address of the array is 200.
p = (char*)(arr+1);
Here p will be 204.
If, p = (char*) arr+1;
Then it will be 201.
In some compiler, you can equate two different data type pointers so you have to typecast them.
Let's say the base address of the array is 200.
p = (char*)(arr+1);
Here p will be 204.
If, p = (char*) arr+1;
Then it will be 201.
Siva said:
1 decade ago
#include<iostream>
int main()
{
using namespace std;
int a[]={1,2,3};
int *p;
p=a;
p=p+1;
cout<<*p;
return 0;
}
This will return 2, why? It is similar to that of the above question.
int main()
{
using namespace std;
int a[]={1,2,3};
int *p;
p=a;
p=p+1;
cout<<*p;
return 0;
}
This will return 2, why? It is similar to that of the above question.
Rupinderjit said:
1 decade ago
Viraj is right.
If you want to get 3 as output, use (p+2) and proper typecasting.
Because: p+1 points to upper byte of 3 as per little-endian method, which is 0 (00000000 00000011) and p+2 points to 3, lower bye of three (00000000 00000011).
If you want to get 3 as output, use (p+2) and proper typecasting.
Because: p+1 points to upper byte of 3 as per little-endian method, which is 0 (00000000 00000011) and p+2 points to 3, lower bye of three (00000000 00000011).
Shivani Mundra said:
5 years ago
It will give a warning instead of error hence p=arr is illegal but this code will compile and give output.
But my question is how to know that these integers will be stored in little-endian firm or big-endian?
Please, anyone, help me.
But my question is how to know that these integers will be stored in little-endian firm or big-endian?
Please, anyone, help me.
Loki said:
1 decade ago
The answer will give value based on whether it is little endian or big endian machine. Both p and p+1 points to address range or value 2. If any answer given the please explain or give note about differences on different machine.
Rahul p said:
1 decade ago
@viraj
Your answer is correct. But here integer requires 4 bytes of storage
Hence, when
p = (int*)(p+4); It will print next element of arr which is 3.
&
p = (int*)(p+8);It will print next element of arr which is 4.
Your answer is correct. But here integer requires 4 bytes of storage
Hence, when
p = (int*)(p+4); It will print next element of arr which is 3.
&
p = (int*)(p+8);It will print next element of arr which is 4.
Vartika said:
1 decade ago
p = (int*)(p+1);
printf("%d",*p);
This should give the output as 3 because %d access 2 bytes of memory so even if we start accessing the memory from 2nd byte from the left, answer should be 3 i.e. 00000000 00000011.
printf("%d",*p);
This should give the output as 3 because %d access 2 bytes of memory so even if we start accessing the memory from 2nd byte from the left, answer should be 3 i.e. 00000000 00000011.
Vaishu said:
7 years ago
The above code has displayed suspicious pointer conversion error @line p=(int*)(p+1); in turbo C.
then how it is compiled and displayed output?
Can anyone tell the memory address allocation for the above program?
then how it is compiled and displayed output?
Can anyone tell the memory address allocation for the above program?
(1)
Prateek said:
1 decade ago
p is a char. pointer and it is assigned to complete address of arr array. so when we write (ar+1) it take address next beyond arr array. which having value 0 of defined pointer array.
Prakash N D said:
1 decade ago
Its simple whenever you increment the pointer it will incremented by 4. Because all pointer of size is 4 bytes. Since array contained 4 elements but, last one not initialized the result is zero.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers