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;
}
Discussion:
69 comments Page 6 of 7.
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'.
Sharad salve said:
1 decade ago
Here p contains the base address and that value H is replaced by M.
M@c said:
1 decade ago
If I change , Then what will be the o/p ?
char str[20] = "Hello";
char *const p=str;
// 1)
*p='Ma';
// 2)
*p='Mah';
Tell me for both the cases.
char str[20] = "Hello";
char *const p=str;
// 1)
*p='Ma';
// 2)
*p='Mah';
Tell me for both the cases.
Tulshiram said:
1 decade ago
As given example
char*const t="Hello";
in this statement we have see pointer is constant but String t is not that why next statement
*t='M';
it is a String pointer which point base address is nothing but 'H'
so it will replace with it and give output: "Mello"
char*const t="Hello";
in this statement we have see pointer is constant but String t is not that why next statement
*t='M';
it is a String pointer which point base address is nothing but 'H'
so it will replace with it and give output: "Mello"
M.Bhagya said:
1 decade ago
Here p contains the base address and that value H is replaced by M.
Prasad said:
1 decade ago
Guys a pointer variable whose address never changed is called pointer constant. So here we can't change the address but we can change the value at that address
int x=10;
int *const p=&x;
int y=50;
p=&y;// wrong
And constant pointer means whose value can never be changed
const int *p;
int x=10;
p=&x;
*p=20;// can't modify the existing value..
int x=10;
int *const p=&x;
int y=50;
p=&y;// wrong
And constant pointer means whose value can never be changed
const int *p;
int x=10;
p=&x;
*p=20;// can't modify the existing value..
Seshu said:
1 decade ago
const char* p --> is constant char pointer where the value at address can not be modified.
char *cont p --> is constant pointer holder whose value can not be changed but the value address can be modified.
char *cont p --> is constant pointer holder whose value can not be changed but the value address can be modified.
Yamuna sahadevan said:
1 decade ago
1. const char *p="hello";
Declares a string HELLO as pointer to a constant character which means string can't be changed but the location can be modified.
Declares a string HELLO as pointer to a constant character which means string can't be changed but the location can be modified.
Momo the assasino said:
1 decade ago
1. const char *p="momo";/*here string is constant but pointer is not"
Ex *p="b" no error
But *p="bye" works
2.char const *p; same as 1 another way is this
3.char *const p="hello"
Pointer is constant but not string.
Ex *p="b" no error
But *p="bye" works
2.char const *p; same as 1 another way is this
3.char *const p="hello"
Pointer is constant but not string.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers