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

intis that a pointer can only hold a memory address while anintholds 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.

#pointers #c-programming #programming #preformance #wild-pointer #c++

How To Use Pointers in C
1.05 GEEK