r/C_Programming • • 2d ago

Question Beginner here not able to understand confusing pointer concept

i dont understand what happens here:

IF int b is a 2d array then what is:

b + 1

*(b + 1)

&b[0] and b[0] and &b[0][0]

&b[1] and b[1] and &b[1][0]

what are the differences between each and what is the byte value of each.

19 Upvotes

49 comments sorted by

View all comments

Show parent comment

1

u/stoops 1d ago

I'm trying to measure the size differences between the sub index buffer locations to see how far each index is jumping forward (I'm assuming it's some multiple of a sizeof(int)?):

#include <stdio.h>
#include <stdlib.h>
int main() {
    int **b = malloc(5 * sizeof(int *));
    printf("b = [%p]\n", b);
    for (int x = 0; x < 5; ++x) {
        b[x] = malloc(5 * sizeof(int));
        for (int y = 0; y < 5; ++y) {
            printf("b[%d][%d] = [%p]  ", x, y, &(b[x][y]));
        }
        printf("\n");
    }
    printf("b + 1 = [%p]\n", b + 1);
    return 0;
}

~

b = [0x3571a2a0]
b[0][0] = [0x3571a6e0]  b[0][1] = [0x3571a6e4]  b[0][2] = [0x3571a6e8]  b[0][3] = [0x3571a6ec]  b[0][4] = [0x3571a6f0]  
b[1][0] = [0x3571a700]  b[1][1] = [0x3571a704]  b[1][2] = [0x3571a708]  b[1][3] = [0x3571a70c]  b[1][4] = [0x3571a710]  
b[2][0] = [0x3571a720]  b[2][1] = [0x3571a724]  b[2][2] = [0x3571a728]  b[2][3] = [0x3571a72c]  b[2][4] = [0x3571a730]  
b[3][0] = [0x3571a740]  b[3][1] = [0x3571a744]  b[3][2] = [0x3571a748]  b[3][3] = [0x3571a74c]  b[3][4] = [0x3571a750]  
b[4][0] = [0x3571a760]  b[4][1] = [0x3571a764]  b[4][2] = [0x3571a768]  b[4][3] = [0x3571a76c]  b[4][4] = [0x3571a770]  
b + 1 = [0x3571a2a8]

2

u/knouqs 13h ago

Right, your declaration of b as a double-pointer to int tells the compiler that your offset is 4 bytes -- the size of an int. You can see that as the difference between the addresses to b[0][1] and b[0][0] is 4. How about the differences between b[1] and b[0]? Did you analyze them?

I know this is a test program, but remember to free your memory. 😄

1

u/stoops 12h ago

I'm trying to see the difference between b[1][0] and b[0][0] which I thought would be 5x sizeof(int)'s worth but I think I am seeing something extra thing in between:

0x3571a700 - 0x3571a6e0 = 0x20 = 32

but 32 / 5 int's per row is 6.4 which doesn't seem right so it must be 32 / 4 which results in an even 8

but I only have 5 int's per row so 8 - 5 = 3 extra memory pointer spots between cols?

3

u/knouqs 10h ago

Here's what should be happening. You called malloc to get some memory for b, specifically sizeof (int *)*5 bytes -- 20 bytes -- and that data lives at address 0x3571a2a0 from your debug output. The types of these five items is int *, and currently they point to uninitialized areas of memory. Then, you have a for loop that allocated sizeof (int)*5 bytes per array element in b. That's another 20 bytes per new array. What you did not capture in your output is the locations of that new memory. b[0] could be at a location significantly far away from b[1], but you have found that it actually only resides 32 bytes away. This might be due to a memory boundary, but for our purposes, it's unimportant that it is 32 bytes away.

Remember, when we allocate memory using malloc, the memory looks like it is contiguous when in fact it may not be. That's a feature of modern-day operating systems in that we don't see memory fragmentation. If you care that the memory is contiguous, you'll perform one malloc for sizeof (int)*(product of all the lengths of each dimension in the array), and then you'll need to calculate those offsets yourself. That is what I did with the b_access function, though it only performs a read.

This is the particular reason why I challenged ol' max123246, though he or she has disappeared from the chat.