C, C++ and C++/CLI cheat sheet

No explanation of the basic concepts, just a memory help when you don’t use C/C++ frequently, or if you’re a beginner with some bases.

First question: why pointers? Answers: because functions arguments are passed by value in C and C++. Because arrays and strings are crazy in C.

How to read pointers

Here is a handy way of remembering how pointers syntax works.

In variable declaration

Go from the right to the left of the declaration, from the innermost parenthesis, going back to the right when hitting “)”

  • Read the name of the declaration
  • *  is “a pointer to”
  • []  is “an array of”
  • ()  is “a function returning”
  • Finally, read the type

int *p[]; reads “p is an array of pointers to int”.
int *(*foo())();  reads “foo is a function returning a pointer to a function returning a pointer to int”.

In variable usage

Variable usage reads from left to right.

  • Determine “get” (read variable) or “set” (write variable)
  • *  is “the value pointed to by”
  • &  is “the address of”
  • [i]  is “the element at position i”
  • Read the variable name

*p = 5;  reads “set the value pointed to by p”.
x = &i;  reads “get the address of i”.
printf("%i", *foo[0]);  reads “get the value pointed to by the element at position 0 of foo”.

C pointers

Manipulating a variable through a pointer is called dereferencing. It is used through an indirection expression.
Manipulating pointers through variables is called decaying.

Given an integer:

  • Create an pointer:
  • Get the memory location of the variable:
  • Get the value of the variable:
  • Set the value of the variable:
  • Setting a pointer to null allows to assign it to a variable later:

C arrays

Arrays mostly can’t be manipulated directly, but rather through pointers.
The variable array  is, for most intents and purposes, a pointer to the first element of the array (until it’s incremented).

Given an array of integers:

  • This doesn’t really creates an array. It creates 3 integers side-by-side, and stores the memory location of the first one.
  • array == &array == &array[0]
  • When you pass an array as an argument to a function, you really pass a pointer to the array’s first element, because the array decays to a pointer.
  • Incrementing a pointer to an array really moves the pointer to the next element of the array (the pointer is incremented by the size of the type of the pointer).
  • The subscript operator (the [] in array[0]) has nothing to do with arrays themselves, you can also use it on a pointer:

C structures

Given a structure:

  • Accessing members of the structure through the variable:
  • Accessing the members through a pointer:

C++ references

C++ is much less in love with pointers than C, and prefers using reference variables. They are mostly used in the same way as pointers, but their address can’t change (they always points to the same variable).
The main usage of a reference variable is for function parameters, to pass the variables by reference (by default it’s by value), which allows to not use pointers.

Given an integer:

  • Create a reference to a variable with  int &r = i; ; this is called binding a reference to an object
  • You do not need to dereference a reference to access the variable’s value:
  • To make sure the arguments are passed by reference in a method:
  • On the other hand, the same method using pointers must be passed the variables by reference in the call:

Managed instances

Instances of managed objects should be used through handles ( ^ , also called “hat”). It can be read “is a handle to the instance of the managed class”. Then, you use the handle as if it was the actual instance.

Note that, even though you don’t have to use managed objects in C++/CLI, you have to declare instances of managed objects with a handle.

Managed pointers

Because of garbage collection (which changes memory addresses), you can’t just use a regular C/C++ pointer to a managed objects. The managed pointers are called interior pointers. A good overview can be found here.

The syntax for interior pointers is very… declarative:

Native pointers are automatically converted to interior pointers (the inverse is not true):

As with pointers in C++, usage interior pointers is not recommended, and you should rather use managed references.

Managed references

A managed reference is called a tracking reference and uses  %  instead of & .

Managed pointers and references have some weirdness, for which this SO post goes into further details.

Further readings