C Pointer Syntax in Plain English

Lee Gaines
3 min readSep 28, 2017

--

THIS POST NOW LIVES ON MY BLOG:

http://lee.earth/posts/c-pointer-syntax-in-plain-english/

In this post we will look at some C syntax related to pointers. Pointers are variables that contain an address to a location in memory. One of the primary uses of pointers is for passing values of variables between functions. Without pointers we would only be able to pass copies of values of variables between functions (read-only) in our C programs, and that is very limiting.

Before we examine the syntax of pointers in C, it’s imporant to know that a pointer doesn’t actually exist by itself. A pointer must be attached to a type of data. We should not say “P is a pointer”, but instead say “P is a pointer to a(n) _____” (fill in the blank with a data type).

Here is a simple declaration:

int *p;

“I’m declaring a variable called ’p’. It is a pointer to an integer.”

int n = 100;

“I’m declaring a variable called ’n’. It is an integer. It’s value is 100.”

p = &n;

“I’m assigning the memory address of ’n’ to the pointer variable, ‘p’. P is now pointing to the address of ‘n’”

So far we’ve declare our pointer, p, and our integer n. Now we have set p to point to the address of n. The & is a symbol that refers to a memory address.

printf(“%p\n”, p);

“Print the address of n.”

An easy way to display what is “inside” of a pointer is to use the %p format specifier with printf. This will print a hexadecimal memory address that might look something like this: 0x7ffcd1adeb34. It can be useful to know where your pointers are pointing when you are debugging a program.

*p = 200;

“Go to the address that p is pointing to and put the value 200 in it.”

In this context, *p = 200 would be known as dereferencing. We now have control over the value of n. This is where pointers can be very powerful. We can write a value (even if it’s outside of our local function) in any memory address.

char *str;

“I’m declaring a variable called ‘str’. It is a pointer to a char.”

char *str = “Hello”

“I’m assigning the characters ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, and ‘\0’ to the variable ‘str’.

This could also be written as: char str[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};

‘\0’ is known as the null byte. It’s a character with an ASCII value of 0. The null byte is used to mark the end of a sequence of characters. It let’s us know when our “string” ends.

Underneath the hood, a “string” in C is just an array of characters ending with the null byte.

char c = str[1];

“I’m assigning the 2nd character in the array called ‘str’ to a char variable called ‘c’.”

One way of accessing individual elements of an array is to use brackets next to the variable’s name. This is known as “array notation”. Inside the brackets you can place a number, this refers to the index, or the “position” in the array. Arrays begin with 0, so index 1 refers to the second element in the array.

char c = *(str + 1);

“I’m assigning the 2nd character in the array called ‘str’ to a char variable called ‘c’.”

This syntax looks different than the one above, but means the exact same thing. This is known as “pointer notation”.

To summarize, pointers are very powerful. They allow us to read and write values of variables anywhere in our program. Pointers are always associated with a data type. Strings are arrays of characters followed by a null byte and individual elements of an array can be accessed using pointer or array notation.

I hope you found this post helpful in understanding C pointer syntax. If you have any questions, comments, or suggestions, feel free to hit me up on twitter @eightlimbed.

--

--