Thursday, July 9, 2009

What is a pointer constant in C?

there are three things


pointer to constant - the pointer directs to a constant value


const int *x = 5;


*x = 6;\\invalid


constant pointer - pointer direction cannot be changed


int const * x = 6;


x = 7; or x = %26amp;i; \\is invalid


constant pointer to constant


mix of two above

What is a pointer constant in C?
you mean a pointer to a constant value?





like const int* x = 30;





more details please
Reply:Fig: Can be modified (Y/N)?


const char* :: Pointer(Y) Memory(N)


char *const :: Pointer(N) Memory(Y)


const char *const :: Pointer(N) Memory(N)





Read the declarations inside out. For eg.


const char *p; // p is a pointer to const char


char *const p; // p is a const pointer to char


const char *const p; // p is a const pointer to a const char





---


char nc_hello[] = { 'h', 'e', 'l', 'l', 'o' }; // non const hello


const char *c_hello = "hello"; // const hello





case 1:


const char *p_c_hello = c_hello; // valid


const char *p_nc_hello = nc_hello; // valid


p_c_hello[0] = 'H'; // Invalid; cannot use pointer to modify memory


p_nc_hello[0] = 'H'; // Invalid


p_c_hello = p_nc_hello; // Valid; can change the pointer

survey

No comments:

Post a Comment