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.
Momo the assasino said:
1 decade ago
1. const char *p="momo";/*here string is constant but pointer is not"
Ex *p="b" no error
But *p="bye" works
2.char const *p; same as 1 another way is this
3.char *const p="hello"
Pointer is constant but not string.
Ex *p="b" no error
But *p="bye" works
2.char const *p; same as 1 another way is this
3.char *const p="hello"
Pointer is constant but not string.
Sollu said:
1 decade ago
As p is const variable pointer it cannot be incrementedd or decremented. It will always point to base address.
or
It is because the first position of the str array is replaced by 'M'.
or
It is because the first position of the str array is replaced by 'M'.
or
It is because the first position of the str array is replaced by 'M'.
or
It is because the first position of the str array is replaced by 'M'.
Madhu said:
1 decade ago
const char *p means it is pointer to a constant character
as: p="hi" (legal) but
*p="hi"(illegal)
In char const *p means constant pointer to a character
as: p="hi"(illegal) but
*p="hi"(legal)
as: p="hi" (legal) but
*p="hi"(illegal)
In char const *p means constant pointer to a character
as: p="hi"(illegal) but
*p="hi"(legal)
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;
}
Seshu said:
1 decade ago
const char* p --> is constant char pointer where the value at address can not be modified.
char *cont p --> is constant pointer holder whose value can not be changed but the value address can be modified.
char *cont p --> is constant pointer holder whose value can not be changed but the value address can be modified.
Mangesh Girhale said:
1 decade ago
Please explain me the difference between
1. const char *p="hello";
2. char const *p="hello";
3. char * const p="hello";
4. const char * const u="hello";
1. const char *p="hello";
2. char const *p="hello";
3. char * const p="hello";
4. const char * const u="hello";
Susil said:
1 decade ago
Here p acts as a constant to pointer.
We can change the base address but can't change whole string
/// p = "Bye" // Error: can't modify const object
Here the whole string is constant
We can change the base address but can't change whole string
/// p = "Bye" // Error: can't modify const object
Here the whole string is constant
M@c said:
1 decade ago
If I change , Then what will be the o/p ?
char str[20] = "Hello";
char *const p=str;
// 1)
*p='Ma';
// 2)
*p='Mah';
Tell me for both the cases.
char str[20] = "Hello";
char *const p=str;
// 1)
*p='Ma';
// 2)
*p='Mah';
Tell me for both the cases.
Wikiok said:
1 decade ago
You can change the whole string, but if you write p="Bye", then you assign a new address to the pointer. Instead of theis, you can use loop or do it char by char.
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);
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers