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 1 of 7.
Sagar said:
1 decade ago
Here p contains the base address and that value H is replaced by M.
Uday said:
1 decade ago
It is because the first position of the str array is replaced by 'M'.
Sachin said:
1 decade ago
Yes *p held the 0 index of str. As a result the letter M replaces H in Hello.
Mayur said:
1 decade ago
As p is const variable pointer it cannot be incrementedd or decremented. It will always point to base address.
Sollu said:
1 decade ago
As p is const variable pointer it cannot be incrementedd or decremented. It will always point to base address.
or
It is because the first position of the str array is replaced by 'M'.
or
It is because the first position of the str array is replaced by 'M'.
or
It is because the first position of the str array is replaced by 'M'.
or
It is because the first position of the str array is replaced by 'M'.
Susil said:
1 decade ago
Here p acts as a constant to pointer.
We can change the base address but can't change whole string
/// p = "Bye" // Error: can't modify const object
Here the whole string is constant
We can change the base address but can't change whole string
/// p = "Bye" // Error: can't modify const object
Here the whole string is constant
Solomon said:
1 decade ago
Thank you all fr your explanation.
Wikiok said:
1 decade ago
You can change the whole string, but if you write p="Bye", then you assign a new address to the pointer. Instead of theis, you can use loop or do it char by char.
Vinoth said:
1 decade ago
Thanks all of them
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers