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

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

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

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.

Saroj said:   1 decade ago
If we declare the constant pointer then already declared place is in array a[size], what ever we declare that place depends on the size it should changed.

Rocketraja said:   1 decade ago
Can you explain still more clearly?

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

Saurav said:   1 decade ago
P is const how can we change it?

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

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.

Abhay said:   1 decade ago
1. const char *p : means p is pointer pointing to a constant char i.e. you can not change the content of the location where it is pointing but u can change the pointer itself to point to some other char.

2. char const *p, and char * const p : both are same & in this case p is a constant pointer poiting to some char location. you can change the contents of that location but u can't change the pointer to point to some other location.

Hope i answered the way you wanted !!


Post your comments here:

Your comments will be displayed after verification.