Since pointers are used to store the memory address of a variable, we need to obtain this address first. This is done by using the reference operator (&). The syntax for this is:


Consider the following piece of code:

int x = 9; printf("%p", &x);

This will output to the screen the memory address of the variable x. To assign an address to a pointer, the following syntax is used:

pointer = &variableName;

Consider the following example:

int x = 727; // Declare variable x int* ptr = &x; // Declare a pointer to an int variable and assign to it the address of variable x printf("%p\n", &x); // Print the address of x printf("%p\n", ptr); // Print the address pointed to by ptr

The code above declares an integer variable x and an int pointer variable ptr. The pointer is then assigned the memory of the variable x. The last two printf() statements print this address; both lines will output the same hexadecimal number as they both refer to the same address in memory.

The address a pointer contains is not constant. A pointer may be reassigned to a new address so long as type consistency is maintained (e.g., int pointer points to a variable of type int). Consider this example:

int* ptr; // Declare pointer to an integer type int x = 3; // Declare variable x ptr = &x; // Assigns memory address of variable x to the pointer printf("%p\n", ptr); // Prints address of variable x int y = 14; // Declare variable y ptr = &y; // Reassigns the pointer to the memory address of variable y printf("%p\n", ptr); // Prints address of variable y



Declare a pointer to a double called dblPtr and assign to it the address of variable g.


Print the address of variable g.


Reassign dblPtr to the address of the variable pi.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?