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 9 of 9.
TDas said:
5 years ago
Cannot convert an integer to a character pointer.
So,to get the desired output the revised code should be p=(char*)arr in place of p=arr and p=(char*)(int*)(p+1) in place of p=(int*)(p+1) .
So,to get the desired output the revised code should be p=(char*)arr in place of p=arr and p=(char*)(int*)(p+1) in place of p=(int*)(p+1) .
(2)
TDas said:
5 years ago
Simply p=p+1 works. No need to convert integer pointer then character pointer.
Pushparaj said:
5 years ago
Just, (p+1) is NULL. (p+2) is NULL. (p+2) is NULL. (p+4) is NULL. etc
Otherwise, p=(int*)(p+1) is NULL or ZERO.
Otherwise, p=(int*)(p+1) is NULL or ZERO.
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.
Akash said:
5 years ago
All droughts are cleared, Thanks for explaining @Puru.
Mayur22kar said:
4 years ago
@All.
According to me; the coding part is;
#include<stdio.h>
int main()
{
// create and array hear with name arr
int arr[3] = {2, 3, 4};
// assume array is created at memory location 1000
// so 2 is store at location 1000 and
// 3 on 1004 and
// 4 is store at 1008......
// created one char pointer *p
char *p;
//p is pointing to the 1000
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1); // when you increase hear p+1 then 1000 becomes 1001 because p is character pointer
printf("%d", *p);
return 0;
}
According to me; the coding part is;
#include<stdio.h>
int main()
{
// create and array hear with name arr
int arr[3] = {2, 3, 4};
// assume array is created at memory location 1000
// so 2 is store at location 1000 and
// 3 on 1004 and
// 4 is store at 1008......
// created one char pointer *p
char *p;
//p is pointing to the 1000
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1); // when you increase hear p+1 then 1000 becomes 1001 because p is character pointer
printf("%d", *p);
return 0;
}
(11)
Abdul said:
4 years ago
Well done @Mayur.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers