Pointers in C Programming
In C, a pointer is a variable that ‘points’, or whose value is the address of another variable, not the value of another variable but the direct address of the other variable in memory. Every variable when initialized receives its own ‘address’ in memory, for example :
#include <stdio.h>int main () {
int var1;
int var2; var1 = 4;
var2 = 7; printf("Address of var1 is: %x\n", &var1);
printf("Address of var2 is: %x\n", &var2);
return 0;
}
In the above example we aren’t using any pointers yet, but we can see that var1
has a value of 4
, but when we use &var1
the &
will output the address of the variable and not the value. The output of the above code would look like this :
Address of var1 is: 13c9f3b8Address of var2 is: 13c9f3bc
A pointer will have the value of this hexadecimal number that designates the exact address in memory. When using pointers there are three steps to use them properly. First of all, like any variable, we have to define a pointer variable. To declare a pointer the syntax would look like this :
int* pointerVariable;
This declares a pointer named pointerVariable
with a type of int
. The type must be a valid C data type. The *
is what is used to declare the variable as a pointer it can be written different ways and also can have different data types :
// Both of these examples can be used to declare pointers as well
int *pointerVariable;
int * pointerVariable;// Syntax of different data types
double* doublePointerVariable;
float* floatPointerVariable;
char* charPointerVariable;
The actual data type of the value of pointers is always the same, it will always be the hexadecimal value, that is the exact address location. The only difference in data types when it comes to pointers is the data type of the variable that the pointer is actually pointing to.
The second step to using pointers is to assign the pointer variable the address of another variable that it will point to. This is where you would define which variable your pointer is going to point to within your C program. Here is an example of 1) Defining our pointer, 2) assigning it a variable and 3) accessing the address and value of the pointed-to variable.
#include <stdio.h>int main () { // Declaring regular variables
int var1 = 10;
float var2 = 3.56; // Declaring a pointer variable
int* var1Pointer;
float* var2Pointer; // Assigning the memory address of the variables to the pointers
var1Pointer = &var1;
var2Pointer = &var2; // Accessing the address of the pointed-to variables
printf("Address of var1 is : %x\n", var1Pointer);
printf("Address of var1 is : %x\n", &var1);
/*The above output would give the same hexadecimal number*/ // Accessing the value of the pointed-to variables
printf("Value of var1 is : %d\n", *var1Pointer);
printf("Value of var2 is : %f\n", *var2Pointer); return 0;
}
The exact output of the above example would look like this :
Address of var1 is : c2931138Address of var1 is : c2931138Value of var1 is : 10Value of var2 is : 3.560000
Lets break the example down step by step because there is a lot there. We first declare the pointer variables, then when we assign the memory address to the pointer variables we use &var1
and &var2
, the &
symbol is what gives us the address of the variable instead of the value. After assigning the address to the pointers we can then access that memory address using %x
and either var1Pointer
or &var1
produces the same hexadecimal address. We can also see that we can use pointers not just to store the memory address but they can also be used to access the value of the pointed-to variable. You would access it depending on the data type of the variable that is being pointed to and using the correct format specifier needed for that data type. In the example we used %d
when using *var1Pointer
because the data type of var1
is an integer, and %f
for *var2Pointer
because var2
is a float. To access the value of the pointed-to variable by using the pointer variable you just need to put the asterisk *
before the name of the pointer. The above example can also be shortened a bit if we join both steps of declaring and assigning the pointer variable to one single line like so :
int* var1Pointer = &var1;
float* var2Pointer = &var2;
There is many different and easy concepts with pointers, you can have an array of pointers, you can have a pointer that points to another pointer, you can pass pointers to functions and even have functions return pointers. This is just the basics of pointers we will take a look at some of these other concepts in next weeks blog to continue to expand the understanding of pointers and how they work in the C programming language.