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;
}
Mello
Hello
HMello
MHello
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
69 comments Page 7 of 7.

Vinoth said:   1 decade ago
Thanks all of them

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.

Solomon said:   1 decade ago
Thank you all fr your explanation.

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

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'.

Mayur said:   1 decade ago
As p is const variable pointer it cannot be incrementedd or decremented. It will always point to base address.

Sachin said:   1 decade ago
Yes *p held the 0 index of str. As a result the letter M replaces H in Hello.

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:

Your comments will be displayed after verification.