Pointers and arrays

In school, I had a teacher who once told me:

"No matter how experienced you are, nobody truly understands pointers completely."

No statement could be truer. In standard C, a pointer is a variable whose value points to a location in memory. The problem with standard C is that this location in memory is not associated with a particular type. Instead, the pointer type itself defines the type of memory the pointer is pointing to, as in the following example:

int main(void){    int i;    int *p = &i;}// > gcc scratchpad.c; ./a.out

In the previous example, we created an integer, and then created a pointer and pointed it at the previously-defined integer. We could, however, do the following:

int main(void){    int i;    void *p = &i; int ...

Get Hands-On System Programming with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.