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 1 of 7.
Murthy said:
1 decade ago
#include<stdio.h>
int main()
{
char str[20] = "Hello";//here given char str is hello
char *const p=str; //char * const p="hello" i.e str
char const *p="hello" so
*p='M'; //*p=M so it is changed H to M
printf("%s\n", str); // its prints Mello
return 0;
}
NOTE:
const char *p="hello";
char const *p="hello";
char * const p="hello";
const char * const u="hello";
int main()
{
char str[20] = "Hello";//here given char str is hello
char *const p=str; //char * const p="hello" i.e str
char const *p="hello" so
*p='M'; //*p=M so it is changed H to M
printf("%s\n", str); // its prints Mello
return 0;
}
NOTE:
const char *p="hello";
char const *p="hello";
char * const p="hello";
const char * const u="hello";
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 !!
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 !!
Sushmitha said:
1 decade ago
@Red.
The string str is assigned with str[0]='H', str[1]='e' and so on.
The other way of initialising that string would be
char str[]={'h','e','l','l','o','\0'};
NOTE: The name of the array itself holds the base address of the string.
So when p=str,the base address of the string str[0] is assigned to p.
*p='M' implies that the value at p which is str[0]is changed to 'M', which was previously 'H'.
Hope you got it.
The string str is assigned with str[0]='H', str[1]='e' and so on.
The other way of initialising that string would be
char str[]={'h','e','l','l','o','\0'};
NOTE: The name of the array itself holds the base address of the string.
So when p=str,the base address of the string str[0] is assigned to p.
*p='M' implies that the value at p which is str[0]is changed to 'M', which was previously 'H'.
Hope you got it.
(1)
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.
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.
Mamatha said:
7 years ago
char *const p; here p is a constant pointer so here the value of pointer can change, but the address it pointing is not able to change.
*p='M' //valid
but if we change the address it will give error for example
int main()
{
char str[]="hai" ,str1[]="india";
char *const p=str;
p=str1 //error here changing the address of pointer so it gives error.
}
*p='M' //valid
but if we change the address it will give error for example
int main()
{
char str[]="hai" ,str1[]="india";
char *const p=str;
p=str1 //error here changing the address of pointer so it gives error.
}
(7)
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..
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.
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.
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"
Ravindhranath said:
3 years ago
Here,
p is pointing to str, hence p and str are same.
Now str[0]=p[0]='H'
After changing the first character str becomes str[0]=p[0]='M' (since *p, *str, str[0], p[0], all are same and representing the first character of the str)
So str="Mello" is the final answer.
p is pointing to str, hence p and str are same.
Now str[0]=p[0]='H'
After changing the first character str becomes str[0]=p[0]='M' (since *p, *str, str[0], p[0], all are same and representing the first character of the str)
So str="Mello" is the final answer.
(6)
Himanshu said:
1 decade ago
We are not modifying the string of str ,
Then how it can change.
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
Here we have not change the str[20] = "Hello".
I didn't get,
Please Tell me the reason.
Then how it can change.
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
Here we have not change the str[20] = "Hello".
I didn't get,
Please Tell me the reason.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers