Pointers in C Made Easy

Share This Post

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!

That's weird... something went wrong. Please try again.
Welcome to the R U Coding Me Newsletter!

R U Coding Me Newsletter

Subscribe to our newsletter and stay updated.

We use Brevo as our marketing platform. By Clicking below to submit this form, you acknowledge that the information you provided will be transferred to Brevo for processing in accordance with their terms of use

Are You Coding?

If the answer is no, you’re probably missing out on a large opportunity here. And yes, I said the name!

Our FREE developer resources will help you start programming. Additionally, consider applying for mentorship to accelerate you to your career. Click on the Student button below to get started. It will take you to our available courses and any relevant materials to help get you started.

Ready to take your business to the next level? Creating a robust solution in a short amount of time is hard to do if you’ve never done it before. Plus, why should you juggle yet another project with your business? Click on the Business button below if you’re looking to scale your current business online but don’t want to spend a few years to learn how to do it. It will take you to our services so you can see what would best work for you.

More To Explore
Learn C Programming
Multidimensional Arrays in C With Example

FacebookTweetPinLinkedInEmail Wait, You Said There Wouldn’t Be Any More Pointers! No, if you recall from last time, I mentioned there could be pointers that point

Learn C Programming
Efficiently Debugging C Programs

FacebookTweetPinLinkedInEmail What is Debugging? Overview of Debugging Debugging is an art form, rather than a science. Debugging is the act of removing any issues that

R U Coding Me LLC