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 2 of 7.
Anonymous said:
1 decade ago
Here, str is copied to p and the value of p was changed. So, data in p is of str but not str=p. then if value in p is changed., How the value in str would change?
Red said:
1 decade ago
Why is 'M' given? shouldn't it be "M"?
Chethan said:
1 decade ago
@Himanshu.
The string has got changed because we are accessing it through pointer that is we are directly writing to the address of string.
The string has got changed because we are accessing it through pointer that is we are directly writing to the address of string.
Swetha sathish k said:
1 decade ago
Here pointer is constant, but not the string.
Swetha sathish k said:
1 decade ago
char *const p means p pointer is a constant. but the array is not.
Pankaj pandey said:
1 decade ago
But how it is possible because string constraint never change, if we want to change the base address that is possible.
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.
Karthi said:
1 decade ago
char const*p is represent a constant pointer p not the value it holds so you can change the value but in const char*p is vice versa.
Seema said:
1 decade ago
Here p is constant pointer and not pointer to constant.
So we can change the value of str using p but we can't assign new address to p.
So we can change the value of str using p but we can't assign new address to p.
Sushama said:
1 decade ago
Here p is the base address of str, we only modify the base address to m &then str becomes "mello".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers