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 2 of 9.
Amit said:
8 years ago
Here the answer will 2,0 because they are sting contains the following method of binary of 0001001, 0011100, 00010, 000001, 00010.
And also the memory allocation that add[200], add[201], and define the p+1 pointer *ptr (0+1) **ptr and they bytes store in memory and will be defined 2,0.
And also the memory allocation that add[200], add[201], and define the p+1 pointer *ptr (0+1) **ptr and they bytes store in memory and will be defined 2,0.
Raj said:
8 years ago
If you take ptr as int rather char, then (p+1) gives you ans: let (p+0)=1000 then
(P+1)=1004.
In this example typecast is done to char as char is of 1Byte then by p+1 1byte address will get incremented but the arr is int 4 byte.
So, for char, next byte is 0 so (char*) p+1 is 0.
(P+1)=1004.
In this example typecast is done to char as char is of 1Byte then by p+1 1byte address will get incremented but the arr is int 4 byte.
So, for char, next byte is 0 so (char*) p+1 is 0.
Poornachandra h d said:
8 years ago
@Shivam Varshney : the answer is correct.
p=(char *)((int *)(p)); and p=(char *)(p) ; //both are same
so, let us consider starting address of p is 1000;
when we load p = (char *)p or p = (char *)((int *)(p));
the digit '2' is stored as character in 'p'. And next three bits are left as zero;
when you increment address by +1 execute, we get '0'.//now p is 1001
in the compiler, try by increasing 'p = p+4' and print --- you will get 3 //now p is 1004
(now you try printing p+1)//i.e, 1005 --- you will get zero
and again do 'p = p + 4' //now it is 1008.
what will it print now?
Yes, exactly next digit which is stored as char '4'.
Thank you!
p=(char *)((int *)(p)); and p=(char *)(p) ; //both are same
so, let us consider starting address of p is 1000;
when we load p = (char *)p or p = (char *)((int *)(p));
the digit '2' is stored as character in 'p'. And next three bits are left as zero;
when you increment address by +1 execute, we get '0'.//now p is 1001
in the compiler, try by increasing 'p = p+4' and print --- you will get 3 //now p is 1004
(now you try printing p+1)//i.e, 1005 --- you will get zero
and again do 'p = p + 4' //now it is 1008.
what will it print now?
Yes, exactly next digit which is stored as char '4'.
Thank you!
Shivam varshney said:
8 years ago
The answer is wrong here.
The compiler will produce an error.
The compiler will produce an error.
Pankaj kumar said:
8 years ago
p = (char*)((int*)(p)); can anyone explain this?
(1)
Nikhil said:
8 years ago
Guys run this code, you will get same output.
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = (char*)arr;
//p = (char*)((int*)(p));
printf("%d, ", *p);
p =p+1;
printf("%d", *p);
return 0;
}
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = (char*)arr;
//p = (char*)((int*)(p));
printf("%d, ", *p);
p =p+1;
printf("%d", *p);
return 0;
}
Nawaz khan said:
9 years ago
Excellent solution, Thanks @Viraj.
Muhammad Imran said:
9 years ago
Type casting means?
Muhammad Imran said:
9 years ago
p = (char*)((int*)(p));
Why do this type casting?
Why do this type casting?
DISH said:
9 years ago
Do we need to typecast the pointer every time?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers