696 views
0 votes
0 votes
#include <stdio.h>

int main(void)

{

    int i = 10;  

    int j = 20;

    const int *ptr = &i;

  

    printf("ptr: %d\n", *ptr);

    *ptr = 100;       

                     

  

    ptr = &j;          

    printf("ptr: %d\n", *ptr);

  

    return 0;

1 Answer

1 votes
1 votes

I think program will show error because this line

const int *ptr = &i;// ptr is pointer which will point to the constant integer mean we can not change the change the data of the location which is pointed by ptr pointer because in this line 

 *ptr = 100; // we are changing the data of location which is pointed by ptr pointer. It is read only we can not modify it.

if we remove const  from const int *ptr = &i; then I think program will run , may be 10, 20 value will be printed..

please correct me If I m wrong........

Related questions

0 votes
0 votes
0 answers
4
saurabh111 asked Nov 2, 2018
251 views
in char data type ,plzzz tell me initilization can be seprated from declaration..