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.
Divyaa said:
8 years ago
What will be the answer if the given question is;
Char *p =0;*p='a';
Printf("value of pinter p in %c",*p);
Please explain it.
Char *p =0;*p='a';
Printf("value of pinter p in %c",*p);
Please explain it.
Kriti said:
8 years ago
Here, p is a constant so it points only to the first character of string thereafter value at that position is changed to 'M' hence it prints "MELLO".
Rishikesh said:
9 years ago
String name itself indicates base adress for first character.
Mds said:
9 years ago
char str[20] = "Hello";
char *const p=str; //Hello
*p='M'; //*p ->1st element(base) = Mello
printf("%s\n", str);
char *const p=str; //Hello
*p='M'; //*p ->1st element(base) = Mello
printf("%s\n", str);
Vipinsoni said:
9 years ago
p has base address.
After p has 'M'.
But, we are print str we know that str has 'hello'.
Then, how output-Mello?
After p has 'M'.
But, we are print str we know that str has 'hello'.
Then, how output-Mello?
Nikhita said:
9 years ago
p=str means the base address of str is assigned to variable p.
*p='M' means the first position is assigned as 'M'.
Hence H is replaced my M.
Hence it prints MELLO.
*p='M' means the first position is assigned as 'M'.
Hence H is replaced my M.
Hence it prints MELLO.
Sourabh said:
10 years ago
*p is nothing but *(p+0);
So *p = 'M' will be *(p+0) = 'M'.
So 'M' replaces 'H' from Hello.
So *p = 'M' will be *(p+0) = 'M'.
So 'M' replaces 'H' from Hello.
Ankush mamidwar said:
10 years ago
If I change the code like then what is the output?
#include<stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='Mn';
printf("%s\n", str);
return 0;
}
#include<stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='Mn';
printf("%s\n", str);
return 0;
}
Pk123 said:
1 decade ago
Can any one explain char const line?
Venky said:
1 decade ago
@Nithya.
Str[1] = 'M'.
Str[1] = 'M'.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers