Difference between char* and const char*?

char* is a mutable pointer to a mutable character/string. const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so. For the same reason, conversion from const char * to … Read more

Why is the asterisk before the variable name, rather than after the type?

They are EXACTLY equivalent. However, in int *myVariable, myVariable2; It seems obvious that myVariable has type int*, while myVariable2 has type int. In int* myVariable, myVariable2; it may seem obvious that both are of type int*, but that is not correct as myVariable2 has type int. Therefore, the first programming style is more intuitive.

Correct format specifier to print pointer or address?

The simplest answer, assuming you don’t mind the vagaries and variations in format between different platforms, is the standard %p notation. The C99 standard (ISO/IEC 9899:1999) says in §7.19.6.1 ¶8: p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined … Read more

Is an array name a pointer?

An array is an array and a pointer is a pointer, but in most cases array names are converted to pointers. A term often used is that they decay to pointers. Here is an array: int a[7]; a contains space for seven integers, and you can put a value in one of them with an … Read more

size_t vs. uintptr_t

size_t is a type that can hold any array index. This means that, logically, size_t should be able to hold any pointer type Not necessarily! Hark back to the days of segmented 16-bit architectures for example: an array might be limited to a single segment (so a 16-bit size_t would do) BUT you could have … Read more

Are there benefits of passing by pointer over passing by reference in C++?

Passing by pointer Caller has to take the address -> not transparent A 0 value can be provided to mean nothing. This can be used to provide optional arguments. Pass by reference Caller just passes the object -> transparent. Has to be used for operator overloading, since overloading for pointer types is not possible (pointers … Read more