1

I am trying to understand why the output of this code would be 7 11 and I am lost. What is ptr[1][2] and ptr[2][1] referring to?

#include <stdio.h>
#define SIZE 25
int main()
{
    int a[SIZE];
    int *ptr[5];
    
    for (int i = 0; i < SIZE; i++){
        a[i] = i;
    } //has values 0-24
        
    ptr[0]=a; //ptr[0] is assigned with address of array a.
    
    for (int i=1; i < 5; i++){
        ptr[i] = ptr[i-1]+5; //pp[1] = 5; pp[2] = 6??
    }
    printf("\n%d %d",ptr[1][2],ptr[2][1] ); //output is 7 11
    
    return 0;
}

5
  • 1
    ptr[0]=a assigns &a[0], not a per-se. Commented Nov 18, 2020 at 4:01
  • 1
    ptr[0] = &a[0]. ptr[1] = ptr[0] + 5 = &a[5]. ptr[2] = ptr[1] + 5 = &a[10] and so on. That's what the second loop is doing. Commented Nov 18, 2020 at 4:04
  • @user3386109 How does ptr[2] = &a[10]? Commented Nov 18, 2020 at 4:16
  • You need to recognize int *ptr[5] as an Array of Pointers to int[5] (in other words an array of 5 pointers to int). You need to contrast with with int (*ptr)[5] which is a Pointer to Array of int[5] (in other words, a single-pointer to an array of int with 5-elements). With that distinction, you can look at your loop limits and for each iteration ask "What is being assigned to each pointer?" Commented Nov 18, 2020 at 4:22
  • @mathisfun1234 It may help to add some printf to the code. In the first loop, add printf("&a[%d] = %p\n", i, (void *)&a[i]); and in the second loop printf("ptr[%d] = %p\n", i, (void *)ptr[i]); after the assignment. Commented Nov 18, 2020 at 5:30

2 Answers 2

2

ptr[1] = ptr[0]+5 which refer to a[0+5] = a[5]

ptr[2] = ptr[1]+5 which refer to a[5+5] = a[10]

so

prt[1][2] should be a[5+2] which is a[7] = 7

prt[2][1] should be a[10+1] which is a[11] = 11

Sign up to request clarification or add additional context in comments.

Comments

1

While this is quasi-valid pointer code, it's also not the sort of code you should write in practice because it's going to be quite confusing to keep track of what's going on.

Things like ptr[i-1]+5 is kind of a mess to deal with.

What this does is effectively pluck out values 0, 5, 10, etc. through to 20 and put them into a look-up index of sorts composed of pointers.

Now that you have pointers to a contiguous array you can do sneaky things like ptr[n][i] where that will offset the target by i, but normally you'd just do *ptr[n] to avoid all the extra confusion.

It's worth noting that for simple index lookups it's almost always easier to store offsets than pointers. For example if you had this instead:

int ptr[5];
ptr[0] = 0;

for (int i=1; i < 5; i++) {
  ptr[i] = ptr[i-1]+5;
}

Then later:

printf("\n%d %d",a[ptr[1]],a[ptr[2]]); //output is 7 11

A lot easier to debug, too!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.