T
tommyprep
Hi,
I am new to the C language. Got the doubt which is discussed
frequently.
I have the following piece of code which is compiled without
warnings and run Successfully.
Issue 1 : I can change the value pointed by const char *ptr_ch1;
Issue 2: I can change the Pointer location on char const *ptr_ch2.
/**** Output ****/
ptr_ch1 -> Y <<<<< This is Ok.
Modified ptr_ch1 -> G <<< Why compiler allowed it
without any warning.
ptr_ch2 -> O <<< This is OK
Modifed ptr_ch2 -> E <<< This is also OK
Modified Pointer of ptr_ch2 -> G <<<< Why again compiler
didn't give any error/warning.
/*****************/
Checked it in Linux GCC and DEV-C++
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char ch1= 'Y';
char ch2='O';
const char * ptr_ch1 = &ch1;
char const *ptr_ch2 = &ch2;
printf ("\n ptr_ch1 -> %c \n", *ptr_ch1);
ch1 = 'G';
printf ("\n Modified ptr_ch1 -> %c \n", *ptr_ch1);
printf ("\n ptr_ch2 -> %c\n", *ptr_ch2);
ch2 = 'E';
printf ("\n Modifed ptr_ch2 -> %c\n", *ptr_ch2);
ptr_ch2 = &ch1;
printf ("\n Modified Pointer of ptr_ch2 -> %c \n", *ptr_ch2);
return 0;
}
I am new to the C language. Got the doubt which is discussed
frequently.
I have the following piece of code which is compiled without
warnings and run Successfully.
Issue 1 : I can change the value pointed by const char *ptr_ch1;
Issue 2: I can change the Pointer location on char const *ptr_ch2.
/**** Output ****/
ptr_ch1 -> Y <<<<< This is Ok.
Modified ptr_ch1 -> G <<< Why compiler allowed it
without any warning.
ptr_ch2 -> O <<< This is OK
Modifed ptr_ch2 -> E <<< This is also OK
Modified Pointer of ptr_ch2 -> G <<<< Why again compiler
didn't give any error/warning.
/*****************/
Checked it in Linux GCC and DEV-C++
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char ch1= 'Y';
char ch2='O';
const char * ptr_ch1 = &ch1;
char const *ptr_ch2 = &ch2;
printf ("\n ptr_ch1 -> %c \n", *ptr_ch1);
ch1 = 'G';
printf ("\n Modified ptr_ch1 -> %c \n", *ptr_ch1);
printf ("\n ptr_ch2 -> %c\n", *ptr_ch2);
ch2 = 'E';
printf ("\n Modifed ptr_ch2 -> %c\n", *ptr_ch2);
ptr_ch2 = &ch1;
printf ("\n Modified Pointer of ptr_ch2 -> %c \n", *ptr_ch2);
return 0;
}