cpointers

Are pointers addresses in C?


I always assumed that the & operator gives the address of the memory a variable is pointing to. I am currently looking at the MPI standard documentation and found the following, which confused me.

however, that & cast-expression is a pointer, not an address. ISO C does not require that the value of a pointer (or the pointer cast to int) be the absolute address of the object pointed at --- although this is commonly the case


Solution

  • In practice pointers are addresses (except perhaps on some very obscure platforms), but there are additional restrictions attached to them (the compilers can optimize with the assumption that you don't do certain things, e.g. don't violate strict aliasing).

    The way C/C++ standards document those restrictions is by explaining pointers as some abstract entities (not mentioning addresses at all), and documenting what operations are allowed on them (a subset of what you could if they were just addresses).

    Some people really like to say "pointers are not addresses!" to highlight the existence of those additional restrictions, but this is a rather confusing way to put it, and I prefer the "addresses with some restrictions" mental model.