Matt Pierce

Web Developer

Lesson 5 - Pointers and Input

Before we continue on to the next C/C++ topic we'll have to go over an important concept in computer science: how memory works.

When you store information in a variable like int x, behind the scenes C/C++ finds 4 bytes of memory and keeps track of x always meaning that four bytes. That chunk of memory has an address and as long as C/C++ keeps track of the address, it'll always know where to find the data.

For example, if you create two varialbes int x,y;, C/C++ might put x into memory address 1000 and then, 4 bytes later, put y into memory address 1004. You normally don't have to worry about these things because C/C++ will handle all of it for you.

There are times, however, when you'll need to be able to get the address of a variable, or to store the address, or to find out what's at a given address. That's when you'll need to use a pointer.

Project Setup

We're going to start with a pretty basic main file with a couple of variables:

#include <stdio.h>

int main() {
    int inputNumber;
    char inputString[32];

    return 0;

Creating Pointers

To go along with these variables we'll create some pointer variables as well.

int *pointerNumber;
char *pointerString;

Aside: We won't be doing things in the most efficient way in this lesson. I want to show you a few different ways of working with pointers - so think of this as a little roundabout tour.

Getting and Storing Addresses

You can easily get the address of a variable by using the ampersand & operator.

pointerNumber = &inputNumber;

Strings are a little bit different - arrays and pointers are essentially the same thing in C/C++, so you don't need to use the & operator.

pointerString = inputString;

The thing to remember, however, is that the pointers aren't new variables. The pointers point to the other variables by storing the other variables' addresses.

Getting Input With scanf()

We can use the function scanf() to get input from the user but it works a little bit differently from printf(). First, you can only read one variable at a time. Second, you must give scanf() a pointer to the location where you want the data stored.

Reading numbers is pretty straightforward:

printf("Input a number: "); // no line break
scanf("%d", pointerNumber);

Reading strings is a little bit more difficult since you have to tell scanf() how much space it has to work with. We created a string that's 32 characters long - so we can store 31 (the last character of a string is always a terminator to let C/C++ know that the string has ended).

printf("What's your name? ");
scanf("%31s", pointerString);

Since our pointers point back to the original variables, we can use the original variables to output the information we just read.

printf("You entered the number: %d\n", inputNumber);
printf("Your name is: %s\n", inputString);

This is a very important thing to keep in mind for when we learn to write our own functions. When you pass a variable to a function C/C++ copies the variable. But if you pass a pointer then it really copies the address of the variable.

If your function tries to change a copy, those changes will be lost when the function completes. But if the function has the address of the original variable, it can change the original variable. This is why scanf() asks for an address - so it can save whatever you type.

Pointer Arithmetic

When you add a number to a pointer, it will move forward in memory based on the size of the type. For example if you have an integer pointer int *myPointer then myPointer + 1 will give you the address in memory one integer past myPointer.

In practice, this means that we can use a pointer similarly to how we use an array.

The one difference is that once you've changed the address, you then need to get the information at the address. This is called dereferencing.

You can get the information at an address by using the * operator. *pointerString

printf("The 4th letter in your name is %c\n", inputString[3]);
printf("The 2nd letter in your name is %c\n", *(pointerString+1));


In this lesson we learned to make a pointer, how to get the address of a variable, and how to use pointer arithmetic to navigate an array.

Most imporantly we've learned the concept of sending a pointer into a function if we expect that function to save information.