char *array and char array[]

The declaration and initialization declares a pointer array and make it point to a constant array of 31 characters. The declaration and initialization declares an array of characters, containing 31 characters. And yes, the size of the arrays is 31, as it includes the terminating ‘\0’ character. Laid out in memory, it will be something like this for the … Read more

What is a char*?

This is a char: It can only hold one character! This is a C-string: It can hold multiple characters. Another way to write the above is: The 0 at the end is called the NUL terminator. It denotes the end of a C-string. A char* stores the starting memory location of a C-string.1 For example, … Read more

What does “dereferencing” a pointer mean?

Reviewing the basic terminology It’s usually good enough – unless you’re programming assembly – to envisage a pointer containing a numeric memory address, with 1 referring to the second byte in the process’s memory, 2 the third, 3 the fourth and so on…. What happened to 0 and the first byte? Well, we’ll get to … Read more

Regular cast vs. static_cast vs. dynamic_cast

static_cast `static_cast` is used for cases where you basically want to reverse an implicit conversion, with a few restrictions and additions. `static_cast` performs no runtime checks. This should be used if you know that you refer to an object of a specific type, and thus a check would be unnecessary. Example: In this example, you … Read more

What does “dereferencing” a pointer mean?

Reviewing the basic terminology It’s usually good enough – unless you’re programming assembly – to envisage a pointer containing a numeric memory address, with 1 referring to the second byte in the process’s memory, 2 the third, 3 the fourth and so on…. What happened to 0 and the first byte? Well, we’ll get to … Read more

Pointer Arithmetic

First, the binky video may help. It’s a nice video about pointers. For arithmetic, here is an example: (Note that incrementing a pointer that contains a null pointer value strictly is undefined behavior. We used NULL because we were only interested in the value of the pointer. Normally, only use increment/decrement when pointing to elements … Read more

Return array in a function

In this case, your array variable arr can actually also be treated as a pointer to the beginning of your array’s block in memory, by an implicit conversion. This syntax that you’re using: Is kind of just syntactic sugar. You could really replace it with this and it would still work: So in the same … Read more