What is a `char*`?

It is a pointer to a char. When declaring a pointer, the asterisk goes after the type and before the identifier, with whitespace being insignificant. These all declare char pointers: To make things even more confusing, when declaring multiple variables at once, the asterisk only applies to a single identifier (on its right). E.g.: It is primarily for … Read more

Pointer to 2D arrays in C

Using pointer2 or pointer3 produce the same binary except manipulations as ++pointer2 as pointed out by WhozCraig. I recommend using typedef (producing same binary code as above pointer3) Note: Since C++11, you can also use keyword using instead of typedef in your example: Note: If the array tab1 is used within a function body => this array will be placed within the call stack memory. But the stack size is limited. Using … Read more

set head to NULL (‘NULL’ : undeclared identifier)

As written, NULL isn’t defined in your program. Usually, that’s defined in a standard header file — specifically <cstddef> or <stddef.h>. Since you’re restricted to iostream, if yours doesn’t get NULL implicitly from that header, you can use 0 or, in C++11, nullptr, which is a keyword and doesn’t require a header. (It is not recommended to define NULL yourself. It might work sometimes, but it is technically … Read more

Is C++ Array passed by reference or by pointer?

At worst, your lecturer is wrong. At best, he was simplifying terminology, and confusing you in the process. This is reasonably commonplace in software education, unfortunately. The truth is, many books get this wrong as well; the array is not “passed” at all, either “by pointer” or “by reference”. In fact, because arrays cannot be passed by … Read more

Difference between function arguments declared with & and * in C++

f2 is taking it’s arguments by reference, which is essentially an alias for the arguments you pass. The difference between pointer and reference is that a reference cannot be NULL. With the f you need to pass the address (using & operator) of the parameters you’re passing to the pointer, where when you pass by reference you just pass the parameters and … Read more

Dereference void pointer

printf(“\nlVptr[60 ] is %d \n”, *(int*)lVptr); This will cast the void pointer to a pointer to an int and then dereference it correctly. If you want to treat it as an array (of one), you could do a slightly ugly ((int *)lVptr)[0]. Using [1] is out of bounds, and therefore not a good idea (as … Read more