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

Seema said:   1 decade ago
Here p is constant pointer and not pointer to constant.

So we can change the value of str using p but we can't assign new address to p.

Karthi said:   1 decade ago
char const*p is represent a constant pointer p not the value it holds so you can change the value but in const char*p is vice versa.

Pankaj pandey said:   1 decade ago
But how it is possible because string constraint never change, if we want to change the base address that is possible.

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

Vipinsoni said:   9 years ago
p has base address.

After p has 'M'.
But, we are print str we know that str has 'hello'.
Then, how output-Mello?

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

Abhishek said:   6 years ago
Here p is a constant pointer so we can change the value at 0th position but we can't changes it's address.
(6)

Sudhakar said:   1 decade ago
Here pointer is a constant type so the add stored by this can't be changed but we can change the value.

Sushama said:   1 decade ago
Here p is the base address of str, we only modify the base address to m &then str becomes "mello".

Pruthvi said:   1 decade ago
If p is a pointer to a character constant then how can the character constant value can be changed?


Post your comments here:

Your comments will be displayed after verification.