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.
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)
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)
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)
MOUNICA said:
7 years ago
Please give the detailed description for getting this.
(2)
TDas said:
5 years ago
*p=*p[0]='M' and rest of the character should be as it is.
(1)
Vaishu said:
7 years ago
Can anyone tell, what is the base address? why does not the whole string changed?
(1)
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)
Nithya said:
1 decade ago
How to replace the second letter of the word "hello" by m ?
Dragon said:
1 decade ago
If we put *p=bye then answer is eello.
Can any one explain how?
Can any one explain how?
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";
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers