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.

Mangesh Girhale said:   1 decade ago
Please explain me the difference between

1. const char *p="hello";

2. char const *p="hello";

3. char * const p="hello";

4. const char * const u="hello";

Sagar said:   1 decade ago
Here p contains the base address and that value H is replaced by M.

Chetan said:   1 decade ago
char *const p

Declares a constant pointer to a character. The location stored in the pointer cannot change, whereas the value stored at that position can change. You cannot change where this pointer points.

const char * P

Declares a pointer to a constant character. You cannot use this pointer to change the value being pointed to, whereas you can change the address to which the pointer points to.

Mohit Chandra said:   1 decade ago
Hello guys,

If we write
*p='MZ' instead of *p='M'

Still program runs,,,,,, but o/p is same....

Can anybody tell me why....???
and where this is "z" go?

Raghu said:   1 decade ago
Hello guys,

Can anybody tell me ?Is it possible to modify base address?

Zohra said:   1 decade ago
Here *const p=str
str=Hello
=> *const p=Hello
But *p='M'(given)
=>*p=str
=>str=Mello

Madhu said:   1 decade ago
const char *p means it is pointer to a constant character
as: p="hi" (legal) but
*p="hi"(illegal)

In char const *p means constant pointer to a character
as: p="hi"(illegal) but
*p="hi"(legal)

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 !!

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.


Post your comments here:

Your comments will be displayed after verification.