0

I'm new to C/C++ Programming and I'm still learning some differences between pointer datatypes. But I struggled with these one:

int *const x1;
const int *x2;
char *x3[3];
char (*x4)[3];
double* x5, x6;

I think the first one should be a integer variable that points to a constant memory address
And x2 should be a integer Pointer which is const, right?

I don't know if thats right. So can anyone explain me the differences between them?

Haris
  • 12,120
  • 6
  • 43
  • 70
Velox
  • 11
  • 2
  • Note: that `int *a` is a pointer, `int a[2]` is also a pointer (array) that pointes to 2 values. `int *a[2]` is a pointer to array. It may help you a bit to understand – Hearner Jun 13 '16 at 11:24
  • 1
    @Hearner: `int a[2]` is **not** a pointer (although it may *decay* to a pointer in certain cases). – Paul R Jun 13 '16 at 11:30
  • 1
    @Hearner `int *a[2]` is an array of pointers. – Klas Lindbäck Jun 13 '16 at 11:30
  • @SouravGhosh What does UB stand for? – Cheiron Jun 13 '16 at 11:32
  • As mentioned: No, `x1` is a constant pointer to a non-constant integer. And `x2` is a non-constant pointer to a constant integer. And so on. For most cases, you can just read the declarations backwards. I think this is a basic feature of most good tutorials to these languages, but maybe your one missed it out... – underscore_d Jun 13 '16 at 11:38
  • 1
    _"Thanks in advice"_ That's a new one – Lightness Races in Orbit Jun 13 '16 at 11:41
  • 1
    @Cheiron I _think_ Sourav was indicating that trying to apply identical concepts to both languages is likely to result in mishaps - by subtly using the metaphor of how the postincrement is unsequenced and therefore, if one were literally trying to evaluate the result of dividing a variable named `C` by (itself postincremented), anything could happen. – underscore_d Jun 13 '16 at 11:42

4 Answers4

3
int *const x1;  // const pointer to int
                // x1 is const pointer, so it cannot point to anything else (once initilized),
                // but what it points to can changed

const int *x2;  // pointer to const int
                // x2 can points to something else,
                // but what it points to cannot be changed

char *x3[3];    // array of three pointers to char
                // (array of pointers)

char (*x4)[3];  // pointer to array of three char
                // (single pointer)

double* x5, x6; // x5 is pointer to double, x6 is just a double
artm
  • 17,291
  • 6
  • 38
  • 54
2

For problems such as this you can check with cdecl, e.g. for your third example:

char *x3[3];

cdecl returns:

declare x3 as array 3 of pointer to char

and for your fourth example:

char (*x4)[3];

cdecl returns:

declare x4 as pointer to array 3 of char

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    cdecl does not work with multiple declarations (e.g. `double* x5, x6;`). – Holt Jun 13 '16 at 11:30
  • 3
    @Holt: true - you would need to apply a little common sense to that one and not get caught out by the associativity of the `*` - I wasn't planning to do *all* the OP's homework for him though. ;-) – Paul R Jun 13 '16 at 11:32
  • 1
    Thank you for this website. Did not know that there is such a thing. – Velox Jun 13 '16 at 11:36
2

You read C declarators backwards. "int * const" is therefore a constant pointer to int (you can change the int, but not make the pointer point elsewhere after initialization); whereas "const int *" is a pointer to a constant int (change the pointer all you like, but not the int it's pointing to).

"The C declarator syntax is an experiment that failed." according to Bjarne Stroustrup.

Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51
2
 int *const x1;

constant pointer to int, that means the pointer cannot be changed to point anything.

const int *x2;

pointer to int constants. Here the integer is constant, which means you cannot change the value.

char *x3[3];

array (of length 3) of pointers to char

char (*x4)[3];

pointer to an array (of length 3) of char

double* x5, x6;

x5 is pointer to double, x6 is just a double variable

Haris
  • 12,120
  • 6
  • 43
  • 70