top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What's the difference between const char *p, char const *p, and char * const p?

+1 vote
1,629 views
What's the difference between const char *p, char const *p, and char * const p?
posted Jul 4, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

const char *p and char const *p , both are pointer to constant. It means pointer can't modify the content of location where it points, but pointer can point to something else.

While char *const p signifies that "pointer is constant", it can't point to something else.

answer Jul 4, 2014 by Rupam
+1 vote

const char *p - here p is pointer to a constant which means using p you cannot change the value of variable.

int main(void)
{
    char var1 = '0';
    const char* p = &var1;
    *p = 'a'; // Invalid operation as p is pointer to a constant
} 

char const *p - here p is a constant pointer and cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

int main(void)
{
    char var1[] = {'0','1'};
    const char* p = &var1[0];
    p++; // Invalid operation as p is a constant pointer
} 

const char * const p - here p is a constant pointer to a constant which means that p can not point to another variable neither change the value of the variable it point to.

answer Jul 4, 2014 by Salil Agrawal
Similar Questions
+2 votes

As Per my understanding, value can be changed within a code and const means value should never be changed.
So,My question is the meaning/use of taking variable as const volatile?

0 votes

Trivial question, please help.

I need to convert a string literal like "111.25" to floating point number.
atof() fails me as it returns 1111 and discards .25?

...