C: Can you declare an array with constant address but mutable elements?

4263 views c

The C Programming Language

It is not uncommon to define constant pointers to non constant (i.e., mutable) values. So if you do not expect an array to move, but its content to change:

  1. Can you define an array with constant (const) address, but mutable elements?
  2. If yes, then how?

answered question

That defines all arrays of non-constant elements. Once an array have been created, it's in a fixed location.

Short answer: pointers (to which you can assign an address) and arrays (to which you cannot assign an address) can often be used interchangeably. To answer your question: perhaps a const pointer might be a good solution. Look here for more details: stackoverflow.com/questions/1143262

Klorax, int a[42]; is an example of "an array with constant (const) address, but mutable elements". What do you want that is different from that?

1 Answer


Arrays don't shift location in the memory but if you want an array of constant pointers (who can't point to any other values other than what has been used to initialize them) who point to mutable values then (perhaps) use this:

#include <stdio.h>

int main(void) 
    int i=0;
    int j=9;
    int *const ptr[2]={&i,&j};
    printf("%d %d",*ptr[0],*ptr[1]);
    return 0;


posted this

Have an answer?


Please login first before posting an answer.