Reading C / C++ Types

Some notes on reading C and C++ types (go right, and then left, then right … )

First a quick summary from another great link

https://eli.thegreenplace.net/2008/07/18/reading-c-type-declarations

  1. Go right until a closing parenthesis: To read the modifiers, go to the right from the variable name as long as you can – that is, until you run into a semicolon or a closing right parenthesis.
  2. Go left until an open parenthesis or basic type (end with a basic type): When you reach one of these stops, start going left until you reach an opening left parenthesis (or the basic type, in which case you’re done)
  3. New modifier: Each time you see a new modifier (either going right or left), attach it to the end of the current declaration sentence.
  4. Const pointers: see below

 

Here’s a code example based on the simplest example in the example above

 

 

#include
using namespace std;
int main(){
  int *array_ptr[5];
  int (*ptr_array)[5];
  int array[5];
  int test = 3;
  for (int i = 0; i < 5; i++){
    array[i] = 2;
    array_ptr[i] = &test;
  }
  ptr_array = &array;
  for (int i = 0; i < 5; i++){
    cout << "ptr_array: " << (*ptr_array)[i] << endl;
    cout << "array: " << array[i] << endl;
    cout << "array_ptr: " << (*array_ptr[i]) << endl;
  }
}

 

Here's the output from the simple program

ptr_array: 2

array: 2

array_ptr: 3

ptr_array: 2

array: 2

array_ptr: 3

ptr_array: 2

array: 2

array_ptr: 3

ptr_array: 2

array: 2

array_ptr: 3

ptr_array: 2

array: 2

array_ptr: 3

 

This shows the different types

array_ptr (array of pointers to integers with the array size being 5)

ptr_array (pointer to array of 5 integers)

array (array of 5 integers)

dereference array gives pointer to array (ptr_array)

 

Modifiers

const char * a;

if we do &a, the type is const char** . The value stored at *a can’t be changed.

const char * a;

*a = ‘s’;

WILL NOT work.

However, we can change the pointer

const char * a;

char c = ‘s’;

a = &c;

this WILL WORK.

char * const a;

The pointer is a const, but the value can be modified.

*a = ‘s’; WILL compile.

 

https://stackoverflow.com/questions/890535/what-is-the-difference-between-char-const-and-const-char

Again, same principle, read the type from right to left

 

Rule of thumb: read the definition from right to left!


const int *foo;

Means “foo points (*) to an int that cannot change (const)”.
To the programmer this means “I will not change the value of what foo points to”.

  • *foo = 123; or foo[0] = 123; would be invalid.
  • foo = &bar; is allowed.

int *const foo;

Means “foo cannot change (const) and points (*) to an int“.
To the programmer this means “I will not change the memory address that foo refers to”.

  • *foo = 123; or foo[0] = 123; is allowed.
  • foo = &bar; would be invalid.

const int *const foo;

Means “foo cannot change (const) and points (*) to an int that cannot change (const)”.
To the programmer this means “I will not change the value of what foo points to, nor will I change the address that foo refers to”.

  • *foo = 123; or foo[0] = 123; would be invalid.
  • foo = &bar; would be invalid.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s