C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 4)
4.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
return 0;
}
Discussion:
69 comments Page 5 of 7.
Zohra said:
1 decade ago
Here *const p=str
str=Hello
=> *const p=Hello
But *p='M'(given)
=>*p=str
=>str=Mello
str=Hello
=> *const p=Hello
But *p='M'(given)
=>*p=str
=>str=Mello
Sourabh said:
10 years ago
*p is nothing but *(p+0);
So *p = 'M' will be *(p+0) = 'M'.
So 'M' replaces 'H' from Hello.
So *p = 'M' will be *(p+0) = 'M'.
So 'M' replaces 'H' from Hello.
Vaishu said:
7 years ago
Can anyone tell, what is the base address? why does not the whole string changed?
(1)
Sachin said:
1 decade ago
Yes *p held the 0 index of str. As a result the letter M replaces H in Hello.
Deepika said:
6 years ago
The First position of array is replaced by M so it prints "Mello".
Jagadish said:
1 decade ago
As p is a constant pointer how we can change the value when it is assigned?
Bijan said:
1 decade ago
Any one can explain second line
char *const p=str;
Or output of second line
char *const p=str;
Or output of second line
Raghu said:
1 decade ago
Hello guys,
Can anybody tell me ?Is it possible to modify base address?
Can anybody tell me ?Is it possible to modify base address?
Uday said:
1 decade ago
It is because the first position of the str array is replaced by 'M'.
Sagar said:
1 decade ago
Here p contains the base address and that value H is replaced by M.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers