Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   regarding const pointer (http://www.go4expert.com/forums/regarding-const-pointer-t7922/)

answerme 19Dec2007 11:20

regarding const pointer
 
Code:

int i=10;
        const int ci=123;
        const int *cpi;
        int *ncpi;
        cpi=&ci;
        printf("Main cpi  %d\n",*cpi);
        ncpi=&i;
        printf("Main ncpi %d\n",*ncpi);
        cpi=ncpi;
        printf("second %d\n",*cpi);
        ncpi=(int *)cpi;
        printf("%d\n",*ncpi);
        *ncpi=0;
        exit();

My question is why *ncpi =0 ,

Salem 19Dec2007 19:44

Re: regarding const pointer
 
Can you post a complete program, including
- the main(),
- the include files,
- how you compiled it,
- and what you saw on the output (copy/paste from your console).

For instance, C and C++ handle 'const' differently.

answerme 20Dec2007 15:30

Re: regarding const pointer
 
Code:

#include<stdio.h>
main()
{
        int i=10;
        const int ci=123;
        const int *cpi;
        int *ncpi;
        cpi=&ci;
        printf("Main cpi  %d\n",*cpi);
        ncpi=&i;
        printf("Main ncpi %d\n",*ncpi);
        cpi=ncpi;
        printf("second %d\n",*cpi);
        ncpi=(int *)cpi;
        printf("%d\n",*ncpi);
        *ncpi=0;
        exit();
}

output
Code:

Main cpi  123
Main ncpi 10
second 10
10

if it dont include *ncpi=0 it gives the same result

Salem 20Dec2007 19:09

Re: regarding const pointer
 
Why would attempting the assignment AFTER the value has been printed have any effect whatsoever?

Does exit(); really compile for you?

answerme 21Dec2007 09:19

Re: regarding const pointer
 
its a book example ,thats why i asked why it is required to do so ,even i couldnt get that


All times are GMT +5.5. The time now is 11:32.