Pointers in C
What are pointers? Why do we even need these? Why can’t I find my wallet? Great questions, let’s dive in!
Pointers are a type of data type we can use in C. They are unique from the other data types we’ve covered so far. Rather than storing a value in memory, they store an address. This address is a fixed 32 (or 64 depending on certain systems) bit space within the working RAM of the program. This memory address will reference another variable, kind of like “knowing a guy”.
In C, you can “know a guy, that knows a guy, that knows a guy, …” and so on with multiple pointers! But we’re getting ahead of ourselves, let’s dive into some materials before we get too much further:
Pointers are variables that store the address of another variable. For instance, if you want to pass a note to your friend in class but cannot directly hand it to them, you can ask someone else who is sitting next to them to hand the note.
// Pointer Diagram
// In this diagram, we arbitrarily select the 0x459 position in RAM.
// This partition of RAM contains the address of another variable.
0x459
+---+
|0x4|
+---+
When we dereference the pointer, we can go to the location in memory and read/edit the value there like so:
#include <stdio.h>
int main(void)
{
int a = 5;
int *b;
// & means address in C
// "Store the address of a in pointer b"
b = &a;
// Dereference b to get value stored at a
printf("Value at a: %d\n", *b);
return 0;
}
We can use pointers to partition chunks of memory into arrays. Arrays are very useful for keeping relevant information together. If you’ll recall from the function activity, we had to type out individual variables for each student grade. With arrays, we can use one variable and have different positions for each student, much like the following:
0x1 0x2 0x3 0x4 0x5 0x6 ...
+---+---+---+---+---+---+
| 2 | 4 | 6 | 8 |10 |12 | ...
+---+---+---+---+---+---+
Wait, Arrays are Pointers in C?
Always have been… Using Pointers in C, we can specify a fixed location in RAM and allocate a fixed number of blocks that follow it. This allows our values to be closely grouped in RAM, making array accesses lightning fast. We can also use the name of the array variable to pass the entire data structure into functions too!
Arrays are a great way to package relevant information together. This makes it easier to create functions that take arrays (and their lengths) as well as integrate looping mechanics so that you work with one array to go through the entire structure. Arrays require a consecutive chunk of RAM in order to be used. While this is not much of a concern with modern hardware, it is something that should be considered.
We will have quite a few projects coming up involving arrays, so let’s dive into some examples you can try out here!
#include <stdio.h>
int main(void)
{
double scores[3];
scores[0] = 100.0;
scores[1] = 99.0;
scores[2] = 85.5;
int i;
double sum = 0.0;
for (i = 0; i < 3; i++)
sum += scores[i];
sum /= 3.0;
printf("Average of values %f\n", sum);
return 0;
}
#include <stdio.h>
// See if you can fix the issue in this program
int main(void)
{
char *string = "Best language ever!";
string[5] = '\0';
printf("*%s*\n", string);
return 0;
}
Arrays are a lot of fun once you start using them. Try optimizing some of the earlier projects with functions and arrays before the section quiz!
Founder and CEO of R U Coding Me LLC. Jacob obtained his Bachelor’s of Computer Science at the University of Central Florida. He likes to go to the gym and teach people about technology.