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.
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)
TDas said:
5 years ago
*p=*p[0]='M' and rest of the character should be as it is.
(1)
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)
Deepika said:
6 years ago
The First position of array is replaced by M so it prints "Mello".
MOUNICA said:
7 years ago
Please give the detailed description for getting this.
(2)
Vaishu said:
7 years ago
Can anyone tell, what is the base address? why does not the whole string changed?
(1)
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)
Riyaz said:
7 years ago
Thanks for all your explanation.
Akshay said:
7 years ago
Good explanation @Biswajit.
Cp ahirwar said:
8 years ago
Statement 1: char *a="Hello" represents a pointer to constant string.
Statement 2: char a[]="Hello" represents constant pointer to a string.
Statement 2: char a[]="Hello" represents constant pointer to a string.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers