33

I am going over C and have a question regarding const usage with pointers. I understand the following code:

const char *someArray

This is defining a pointer that points to types of char and the const modifier means that the values stored in someArray cannot be changed. However, what does the following mean?

char * const array

Is this an alternate way of specifying a parameter that is a char pointer to an array named "array" that is const and cannot be modified?

Lastly, what does this combination mean:

const char * const s2

For reference, these are taken from the Deitel C programming book in Chapter 7 and all of these are used as parameters passed to functions.

6 Answers 6

82

const char* is, as you said, a pointer to a char, where you can't change the value of the char (at least not through the pointer (without casting the constness away)).

char* const is a pointer to a char, where you can change the char, but you can't make the pointer point to a different char.

const char* const is a constant pointer to a constant char, i.e. you can change neither where the pointer points nor the value of the pointee.

Sign up to request clarification or add additional context in comments.

2 Comments

You've got no idea how many times I've checked this answer... LOL! Many thanks!
@kazbeel Easy rule to remember: const always makes const what is left to it, unless nothing is left to it, then it makes const what is right to it, since const char * and char const * are in fact the same expression. If left to it is an asterisk, it makes the pointer const but not the value, if left/right to it is a type, it makes the value itself const but not the pointer.
33

From what is the difference between const int*, const int * const, int const *:

Read it backwards...

int* - pointer to int
int const * - pointer to const int
int * const - const pointer to int
int const * const - const pointer to const int

Comments

14

In short, it's the difference between:

  • A pointer to a constant.
  • A pointer which itself is constant.
  • A pointer which is constant, and points to a constant.

Here's some code to demonstrate.

// pointer to a const
void f1()
{
    int i = 100;
    const int* pi = &i;
    // *pi = 200; <- won't compile
    pi++;
}

// const pointer
void f2()
{
    int i = 100;
    int* const pi = &i;
    *pi = 200;
    // pi++; <- won't compile
}

// const pointer to a const
void f3()
{
    int i = 100;
    const int* const pi = &i;
    // *pi = 200; <- won't compile
    // pi++; <- won't compile
}

Comments

11

You should try out cdecl:

~ $ cdecl
Type `help' or `?' for help
cdecl> explain const char *someArray
declare someArray as pointer to const char
cdecl> explain char * const someArray
declare someArray as const pointer to char
cdecl> explain const char * const s2
declare s2 as const pointer to const char
cdecl>

4 Comments

Is this a Linux-only utility ?
I have never tried to use it otherwise, but I'm sure it's available for other operating systems, as part of cygwin or otherwise.
Hmm. Ok, I'll see if there's a Mac build or if it comes with XCode. Thanks!
There is also cdecl.org if using the binary isn't an option
3
char * const array;

It means that the pointer is constant. Also,

const * const char array;

means a constant pointer to constant memory.

Comments

0

Repeating what other users wrote, but I want to provide context.

Take these two definitions:

void f1(char *ptr) {
    /* f1 can change the contents of the array named ptr;
     * and it can change what ptr points to */
}
void f2(char * const ptr) {
    /* f2 can change the contents of the array named ptr;
     * but it cannot change what ptr points to */
}

Making the pointer itself const, like in the f2 example, is absolutely almost pointless. Every parameter passed to a function is passed by value. If the function changes that value, it only changes its local copy and has no effect on the calling code.

/* ... calling code ... */
f1(buf);
f2(buf);

In either case, buf is unchanged after the function call.


Consider the strcpy() function

char *strcpy(char *dest, const char *src);

One possible implementation is

char *strcpy(char *dest, const char *src) {
    char *bk = dest;
    while (*src != '\0') {
        *dest++ = *src++;
    }
    *dest = *src;
    return bk;
}

This implementation changes both dest and src inside the function only. Making either of the pointers (or both) const would gain nothing for the strcpy() function or to the calling code.

4 Comments

You have an error in the comment inside f2. "can" should be "can't"
@ManuelSelva ... try void f2(char * const ptr) { ptr[1] = 'a'; } and char foo[] = "Hi"; f2(foo); puts(foo); ... ideone.com/eHwOuW
You edited the code since yesterday, right ;-) ? Sorry for my comment, I read your code wrongly.
No worries @ManuelSelva. Thanks for paying attention and giving feedback!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.