March 11, 2020

Understanding pointers and references in C++

The C++ programming language is hard to understand. In contrast to modern OOP languages like Java, C++ knows many difference variables like normal variables, pointers, references and many more. Even the official manual isn't able to explain the details. The good news is, that a simple look into a C manual will help a lot to get the details.

But let us go a step backward: there are two different programming languages available: C and C++. Most software written in a Linux distribution wasn't created in C++ but in the normal C language. C++ is only teached in the books but seldom used in reality. The obvious difference has to do with object oriented programming. A while a go a stackoverflow users has asked how to realize classes in C:

https://stackoverflow.com/questions/1403890/how-do-you-implement-a-class-in-c

The answer was, that in a struct function pointers needs to be created. And yes, this explanation makes sense. And it explains why C++ is hard to grasp. Because C++ is doing the same but struggles in explaining the reason why. Suppose, somebody likes to program a state of the art program for the console or for the GUI, then the plain C language is the optimal choice. The funny thing is, that even the programmer is asked to declare function pointers the sourcecode is easy to read. If the plan is to not using pointers at all, the python language is a good attempt for software prototyping. It provides classes without pointers and is documented very well.

Instead of arguing against C++ we should ask why the language isn't used in reality more frequently.

But let us go back to the stackoverflow post with the function. A struct is a standard datatype in the C language. It allows to combine different variables into a new one. Extending a struct with functions is the logical next step towards advanced software. The work hypothesis is, that this kind of OOP technique is not an example for bad programming style, but it's the recommended way in programming modern software. The next interesting aspect is, that 95% of C++ programs have the same syntax. That means, pointers are used everywhere. The difference is that a C program which contains of structs and function pointers makes sense while the C++ doesn't makes sense for the newbie.

In the basic version, no function pointers are used, but the struct is provided as a pointer to a normal function:

#include "stdio.h"
typedef struct {
  int x;
  int y;
} Point;
void set(Point* p, int x, int y) {
  printf("set\n");
  p->x=x;
  p->y=y;
};
void show(Point* p) {
  printf("show %d %d\n",p->x,p->y);
};
int main()
{
  Point p;
  set(&p,10,5);
  show(&p);
}

In another stackoverflow post it was explained how to improve the struct with a function pointer. https://stackoverflow.com/questions/17052443/c-function-inside-struct But the answer says, that this is seldom used in reality. That means, the standard way of emulating OOP features in C is to define the function outside the struct but in the same .c file and call the function with a pointer of the struct instance.