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 3 of 7.
Mohit Chandra said:
1 decade ago
Hello guys,
If we write
*p='MZ' instead of *p='M'
Still program runs,,,,,, but o/p is same....
Can anybody tell me why....???
and where this is "z" go?
If we write
*p='MZ' instead of *p='M'
Still program runs,,,,,, but o/p is same....
Can anybody tell me why....???
and where this is "z" go?
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.
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?
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".
Yamuna sahadevan said:
1 decade ago
1. const char *p="hello";
Declares a string HELLO as pointer to a constant character which means string can't be changed but the location can be modified.
Declares a string HELLO as pointer to a constant character which means string can't be changed but the location can be modified.
Saroj said:
1 decade ago
If we declare the constant pointer then already declared place is in array a[size], what ever we declare that place depends on the size it should changed.
Vivekmk said:
1 decade ago
*p denote the value in the base address so *p="M" will replace the character at base that is it will replace "H" so Mello
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers