How To Use Pointers in C

Written by zeppsan | Published 2020/10/15
Tech Story Tags: c | pointers | pointers-in-c | c-programming | programming | preformance | wild-pointer | pointer-basics

TLDR A pointer is a kind of datatype that keeps track of wherein the computer’s memory a specific value is stored. Pointers are often used when working with bigger sets of data that need to be manipulated or when memory management is very important. The pointer had a rumor for being hard to learn and unnecessary since modern languages handle them for you. But understanding how pointers work is essential for a programmer since they will use them all the time without even thinking about it. To understand this article, you need to know the basics of variables in programming, preferably C.via the TL;DR App

To understand this article, you need to know the basics of variables in programming, preferably C since that is what I will be using.
While attending my first programming course at university, there was a fear among us students about what was to come. The pointer. The pointer had a rumor for being hard to learn and unnecessary since modern languages handle them for you. But understanding how pointers work, is essential for a programmer since they will use them all the time without even thinking about it.
What is a pointer?
A pointer is a kind of datatype that keeps tracks of wherein the computer’s memory a specific value is stored. Think of the pointer as a handle for a variable and without this handle, the computer would lose track of it.
The difference between pointers and a normal data type such as
int
is that a pointer can only hold a memory address while an
int
holds an actual value. This might sound weird, but follow along and I'll make it more clear.
When and why are pointers used?
Pointers are often used when working with bigger sets of data that need to be manipulated or when memory management is very important. Pointers will reduce the amount of memory required to do certain things such as working with recursive functions or when passing big data around.
As you might know, when sending a variable to a function it creates a local copy of the variable in that function instance which could result in big memory waste further down the road. So instead of passing around bigger variable/struct that could be ex. 100 Bytes, we can instead pass around a pointer that contains a reference to the variable/structs memory location. Since a pointer is 8 bytes, this is often beneficial because they use a lot less memory.
How to create a pointer?
To kickstart your understanding, I will jump straight into the code and explain afterward what is happening.
int main() {

	// Declaring and initializing the variable Luke
	int Luke = 22;

	// Declaring the pointer Henry
	int* Henry;

	// Initializing the pointer Henry
	Henry = &Luke;

	printf("The pointer to the variable Luke is: %p", Henry);

	return 0;
}
For demonstration purposes we will use:
Luke the variable
and
Henry the pointer
to understand the concept more easily.
At first, I created a normal int named Luke and assigned the value 22 to it, just as one would normally do.
I then created another
int
but with an
*
(asterisk) immediately after. This signals the computer that the int variable that we just created named Henry, is a pointer that will point at a specific int value in the computer's memory. This could be done with any data type:
#include <stdio.h>


int main() {
	char* name;
	int* age;
	double* radius;
}
When declared, we never gave Henry the pointer any directives about where to point, so right now, Henry is pointing in a random direction. At this point, Henry is whats called a wild pointer. To give Henry somewhere to point, we can assign him the memory location of Luke the variable, by typing
Henry = &Luke
.
When telling Henry where to point, you might have realized that I used a
&
(Ampersand) before Luke the variable. The ampersand is used in front of a variable to get its address in memory. When this step is done, we have successfully created a pointer to the memory address where the value 22 is stored.
The output of the function above would have been something like:
The pointer to the variable Liks is: 00B3F98C
This example was made using an int, however, the same technique applies for floats, chars, structs, and so on.
How does a pointer work?
Now that we know how to create a pointer, let's try to more deeply understand how they actually work, else there would be no point ;)
#include <stdio.h>

int main() {
	int Luke = 22;

	int* Henry;

	return 0;
}
The image & code above are equivalent. We declared and initialized the variable Luke and declared the pointer Henry. Henry is now a wild pointer and to make him point at the same memory address as Luke, we simply set Henry = &Luke as before.
By doing this we have successfully made Henry aware of where in the memory Luke stores his value.
How to use a pointer?
Until now you have learned how to declare and initialize a pointer but not how to use it really.
Let's say that we want to print the value that Henry is pointing at, how would we do such a thing? Take a look below:
#include <stdio.h>

int main() {

	int Luke = 22;
	int* Henry = &Luke;

	printf("The value at the memory location that Henry points at is : %d", *Henry);

	return 0;
}
As you might has noticed I used the
*
in front of the pointer Henry when printing him. This is how we "dereference" a pointer, with other words, actually looking at the value the pointer is pointing at. In this case, we would print the value 22 since that is what Henry is pointing at.
If you are struggling to understand why Henry is pointing at the value 22, I would recommend you to take another look at the images & code snippets above.
Pointers in functions
A pointer can be sent as a function parameter and then be manipulated inside the function and stay that way without having to return the value afterward. Below I will show the difference in working with/without pointers.
Without pointer:
#include <stdio.h>

int main() {
	int Luke = 22;
	Luke = changeValue(Luke);
}

int changeValue(int value) {
	value = 31;
	return value;
}
With pointer:
int main() {
	int Luke = 22;
	changeValue(&Luke);
	printf("%d", Luke);
}

void changeValue(int* value) {
	*value = 31;
}
In the example with using a pointer, we sent the memory address of Luke to the function
changeValue()
and then manipulated the value at the memory location by dereferencing the pointer and setting the value. By doing this, Luke's value is now changed without having to return the value.
Makes it possible to return more values
By using pointers, it's possible to return more than one value from a function. Since we can change the value of a variable by manipulating its memory, we could can do stuff like this:

Summary:

Pointers is a data type that holds memory addresses to values in memory, often other variables or structs. Pointers are used to reduce memory usage and speed up the execution time in applications, especially when working with bigger sets of data.
To create a pointer you will have to put an * after the data type declaration, ex
int* age;
. This is how we tell the computer that we are creating a pointer and not a variable. The same operator is used when dereferencing a pointer to get the actual value that the pointer is pointing at.
When getting the memory address of a variable/struct we use the & operator before the variable name, ex
&age
.
Pointers are very useful when working with bigger sets of data or applications that rely on recursive functions. This is because pointers can increase the execution speed of those programs.

Final words

I hope that this increased your knowledge about pointers and why they are used. The reason for writing this is because I see people struggling with pointers a lot and thought I should try to pedagogically teach how they work.
Programming is not impossible to learn, it is all about time and dedication. Everyone can code, all other statements are wrong.
Thanks for me, I'm Eric, a computer science student at Mälardalens Högskola, Sweden.

Written by zeppsan | Computer Science Student | Swedish | 22
Published by HackerNoon on 2020/10/15