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 5 of 7.

Biswajit said:   1 decade ago
[H][e][l][l][o][w]
10 11 12 13 14 15 (Assume these are address of memory)
here,str=10

char*const p=str (here,p will store 10 that is constant)

so,now *p indicate "H" that has replaced by "M"

note: Base address also is a pointer & constant.

Dharmesh said:   1 decade ago
How can we change the value constant pointer ?

Vivekmk said:   1 decade ago
*p denote the value in the base address so *p="M" will replace the character at base that is it will replace "H" so Mello

Bijan said:   1 decade ago
Any one can explain second line
char *const p=str;
Or output of second line

Sandeep kumar said:   1 decade ago
Thnx guys really helpful. But I want to ask what is the difference between

char *const p;

and

char const *p;.

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.


Post your comments here:

Your comments will be displayed after verification.