1

I am confused on incrementing pointers for arrays and then measuring the difference in bytes for the pointer locations.

I understand that a pointer *p will increment by the datatype's size when moving though a multi-dimensional array. However, when an array, thisArray is used:

int thisArray[ 7 ][ 4 ];

How should thisArrray + 1 be evaluated? Do I increment both rows and columns?

I want to know by by how many bytes apart will these pointers will be in memory.

I know the exact answer will be platform dependent. I am seeking the method of solution, not the exact answer.

Since I can't format code in the comments: I am putting it here:

// Using sizeof

#include <stdio.h>

int main(void) {

int tbl[ 7 ][ 4 ];

// Size of tbl
int x = sizeof(*tbl);
printf ( "%d\n" , x);

// Size of tbl + 1;
int y = sizeof(*tbl+1);
printf( "%d\n", y);

//Size apart in bytes
int z = y - x;
printf("%d\n", z);

return 0;
}
Sam S
  • 13
  • 3
  • 1
    Possible duplicate of [How to use pointer expressions to access elements of a two-dimensional array in C?](http://stackoverflow.com/questions/13554244/how-to-use-pointer-expressions-to-access-elements-of-a-two-dimensional-array-in) – BeyelerStudios Oct 09 '16 at 21:52
  • 1
    The actual answer to the question in the title is `sizeof(*thisArray)`. – Dolda2000 Oct 09 '16 at 22:03
  • @Dolda2000 This looks correct; the compiler provides this error: q1.c:12:20: warning: sizeof on pointer operation will return size of 'int *' instead of 'int [5]' [-Wsizeof-array-decay] #include int main(void) { int tbl[ 7 ][ 4 ]; // Size of tbl int x = sizeof(*tbl); printf ( "%d\n" , x); // Size of tbl + 1; int y = sizeof(*tbl+1); printf( "%d\n", y); //Size apart in bytes int z = y - x; printf("%d\n", z); return 0; } – Sam S Oct 09 '16 at 23:03

2 Answers2

5

how should thisArrray + 1 be evaluated?

When thisArray decays to a pointer, the pointers type is int (*)[4]. The type of thisArray+1 is the same.

Do I increment both rows and columns?

Yes.

If you look at the memory of thisArray using a row and column structure, you have:

thisArray ->       +----+----+----+----+
                   |    |    |    |    |
thisArray + 1 ->   +----+----+----+----+
                   |    |    |    |    |
thisArray + 2 ->   +----+----+----+----+
                   |    |    |    |    |
thisArray + 3 ->   +----+----+----+----+
                   |    |    |    |    |
thisArray + 4 ->   +----+----+----+----+
                   |    |    |    |    |
thisArray + 5 ->   +----+----+----+----+
                   |    |    |    |    |
thisArray + 6 ->   +----+----+----+----+
                   |    |    |    |    |
thisArray + 7 ->   +----+----+----+----+

If you want to use traverse elements of the array, you can either use indices or pointers.

Traversing using indices:

for ( int i = 0; i < 7; ++i )
{
   for ( int j = 0; j < 4; ++j )
   {
       // Use thisArray[i][j];
   }
}

Traversing using pointers:

for ( int (*ptr1)[4] = thisArray; ptr1 < thisArray+7; ++ptr1 )
{
   for ( int* ptr2 = *ptr1; ptr2 < *ptr1 + 4; ++ptr2 )
   {
       // Use *ptr2
   }
}

From a readability point of view, using indices to traverse the array is much more intuitive. I strongly recommend using that.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Would you please explain why it's necessary to specify the `4` in `int (*ptr1)[4]`? Thank you. – Charles Oct 10 '16 at 00:07
  • @c650, that says that `ptr1` is a pointer to an array of 4 `int`s. A pointer to an array of 4 `int`s is a different type than a pointer to an array of 5 `int`s. Hence, you have to be exact. – R Sahu Oct 10 '16 at 21:54
  • A pointer is just an address in memory. So long as you do not go out of bounds, you're fine. Do you have any documentation on this behaviour? – Charles Oct 11 '16 at 14:10
  • @c650, which behavior are you talking about? – R Sahu Oct 11 '16 at 14:37
  • Your needing to specify `[4]`. – Charles Oct 11 '16 at 16:26
  • @c650, To define a pointer to an array of object, you need to specify the size of the array. That's part of the language. The value you use affects how you access the elements of the array and what happens when you increment the array. That's why `thisArray + 1` points to the place where you would like it to point to. If the pointer was of type `int (*)[5]`, it would point one `int` past where it points to. – R Sahu Oct 11 '16 at 16:31
0

When you use an index by itself it will be multiplied by the size as defined by the type. Assuming your int is 4 bytes, then each int will be 4 bytes apart in an array of type int. This is how your second or inner index (4) would work.

Check-out this good answer: How are multi-dimensional arrays formatted in memory?

To understand the first index (7), note that a two dimensional array is actually an array of arrays. In our example the second dimension is four elements wide, so you'll skip 4x4=16 bytes for each increment of your 1st index. So thisArray is an address in memory and [2][3] would add 2x16+3x4=44 bytes. That's the 4+4+3=11th consecutive spot in memory, where each spot is 4 bytes.

Community
  • 1
  • 1
Jason K.
  • 407
  • 4
  • 12