Pointers in C and C++

August 5, 2019   

This isn’t so much a “blog” as some notes.

  • & is the address-of operator, and can be read simply as “address of”
  • * is the dereference operator, and can be read as “value pointed to by”

Its importnat to understand is that the * you use to declare a pointer has nothing to do do with the dereference operator.

When you create a pointer it must be typed so char* mystring or int* myint

There’s lots of silly magic around char* pointers as C uses them for easy sleezy strings by scanning to the '\0' or NULL character.

Some C++ code does this as well buts important to remember that char* blah = "Some String" will result in a string constant

C++’s iostream library cout will automatically dereference and stream char pointers.
If you want to see cout the actual pointer address you need to cast it to (*void) like so:

char* mystring = "Fun Times"

cout << "The address of mystring is: " << (void*) mystring << endl