FAQ: Pointers: Lesson - Pointer Arithmetic

This community-built FAQ covers the “Pointer Arithmetic” exercise from the lesson “Pointers: Lesson”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C

FAQs on the exercise Pointer Arithmetic

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi fellow learners!
Unfortunately, I don’t see any value in pointer arithmetic so far.
Why would anyone want to change memory adresses in a program, so what’s the actual use of this?
And where would this be used?
Thanks in advance!

1 Like

Hi _elwe!

I am also just learning this too, but from my understanding of C and arrays so far, this is really important for handling contiguous blocks of memory, like arrays. Here we have values stored consecutively in memory, e.g. the array int myarray[] = {1, 2, 3, 4}; will use 4 bytes for each integer and store them consecutively in memory. This is really useful for quick access to different elements in the array when we use pointer arithmetic. If we want to get the element at the third index, we don’t need to move through the whole array to get there, we can just take the memory location of the first index, and use our pointer arithmetic as pointer+= 3, which here will move us 3*4=12 bytes on to reach element 3. This sounds trivial for a small array, but for really big arrays it’s a big time saver versus having to move through the whole array just to reach the last element, for instance (we would have to do this for some other data structures that don’t store elements in a contiguous block of memory, and so can’t take advantage of pointer arithmetic, like linked lists). So my (basic) understanding is that it’s useful for quick array access.

I hope that helps (and take it with a pinch of salt as I’m also learning, but this is my understanding for now :slight_smile: )

For anyone that struggled with this too, I had written the code below, which ticked the boxes in the codeacademy exercise, but didn’t show any increment for the pointer when printing.


int main() {
  double myvar = 12.1;
  double* ptr1 = &myvar;
  printf("%p\n", &ptr1);

  // Code for Checkpoint 1 goes here:
  // where I increment - removed so I don't give the whole solution
  printf("%p\n", &ptr1);

  // Code for Checkpoint 2 goes here:  
  // where I decrement - removed so I don't give the whole solution
  printf("%p\n", &ptr1);

Turns out I shouldn’t have used &Ptr1 in my printf statement because this meant I was printing the memory address of the pointer itself, so the pointer of the pointer I suppose. So the correct print was just printf("%p\n", ptr1);