0

I have an array ex:

int array[3][5];

If I want to assign a value to an element in that array, it's simple... ex:

array[1][2] = 8;

However, I want to represent it in a pointer math, would this be correct?

**(array + 5 * 1 + 2) = 8;

In both cases it's looking for the 7th position... I'm just not sure if I need to include the 2nd * outside of (code)

**(code)

EDIT: Just a bit of a follow up. Thank you to everyone who was helping me with this. The confusion that I had came from a mistake in instructor's notes, and after I presented the information you've provided he realized the mistake and agrees that

*(*(array+row)+col) 

or

*(*array+MAX_COL*row+col) 

is the accurate pointer math representation for a two-dimensional array.

The **(array+MAX_COL*row+col) is like array[MAX_COL*row+col], which is used in single dimensional array implementation of multidimensional array, just like James Kanze mentioned in one of the replies to this post (the second one is faster than the first one).

B.K.
  • 9,982
  • 10
  • 73
  • 105
  • 2
    Possible duplicate: http://stackoverflow.com/questions/13554244/how-to-use-pointer-expressions-to-access-elements-of-a-two-dimensional-array-in – antonijn Mar 15 '13 at 08:44
  • That question was for C... I'm not sure it'd be the same for my situation. – B.K. Mar 15 '13 at 08:45
  • What you are doing is the same as in C. Go ahead and use the question referenced to by @Antonijn. – fredrik Mar 15 '13 at 08:47
  • Well, my code looks a bit different from what everyone else is using there... is my version correct of using **(array + MAX_COL * row + col)? – B.K. Mar 15 '13 at 08:49
  • Yeah, I've just never used C, so I'm not sure how many differences the two languages have. I understand that C++ was derived from C, but so was Java, yet they have differences. I'm just afraid to branch off into an explanation with a different language (even if it's the parent language). *Well, the comment I made this reply for was deleted... so.. – B.K. Mar 15 '13 at 09:02
  • 1
    *(*(a+1)+2) = 8; what you do will point at a a[7][0] location which is undefined and wrong. in general *(*(array + row)+column). – Koushik Shetty Mar 15 '13 at 09:03
  • Oh, thank you. I wonder where I got that from.... I'm new, so everything is a blur by now, lol Thanks again. – B.K. Mar 15 '13 at 09:05
  • check H2CO3's answer. you will get to know another way. – Koushik Shetty Mar 15 '13 at 09:07

6 Answers6

5

First thing you need to know is that two dimensional arrays aren't pointers to pointers. They're contiguous in memory, so you can represent them by a pointer to T (T is the base type):

int arr[3][5];
int *p = &arr[0][0];

int array_1_2 = *(p + 5 * 1 + 2);
4

array[1][2] equals *( *(array + 1) + 2).

Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
ZhangXiongpang
  • 280
  • 2
  • 7
3

An example to demonstrate pointer Arithmetic operations synonymous to accessing an Array.

#include<stdio.h>

int main()
{
int array[3][5] = {0};
array[2][3] = 5;
printf( "%d\n",array[2][3]);
printf( "%d\n",*(*(array+2)+3) );
return 0;
} 
Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
2

There are a couple of wrong assumptions in you question: for example, array[1][2] isn't looking at the 7th position; it's looking at the third position in the second array element of array. The type of array is array[3] of array[5] of int; when it converts to a pointer, the resulting type is a pointer to array[5] of int, not a pointer to int. So array + 5 * 1 + 2) refers to the eighth element of an array with only 5 elements; i.e. 35 * sizeof(int) beyond the start of array (and the expression still has type pointer to array[5] of int).

As a general rule, if you want to simulate access into a multidimensional array, you should declare a single dimensional array:

int array[columns * rows];

If you do this, then *(array + i * columns + j) will effectively perform a 2 dimensional indexing.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
2

An alternate representation would be

*(array[1]+5)=8

Let me explain simple analogous code.

int *p[5];

Here p is a pointer that points to the address of first element and p is equivalent to &p[0].

To understand this take an example

#include<iostream.h>    
void main()
{    
int *p[2];
int a=0,b=1;
p[0]=&a;p[1]=&b;
cout<<*p<<endl<<&a<<endl;
cout<<*(p+1)<<endl<<&b<<endl;
cout<<p<<endl<<p+1<<endl;
cout<<**p<<endl<<**(p+1);    
}

The output of the above code is

0x8fbdfff0
0x8fbdfff0
0x8fbdffee
0x8fbdffee
0x8fbdfff2
0x8fbdfff4
0
1

In our case int array[3][5] is equivalent to int *p1[5] int *p2[5] int *p3[5]. where p1,p2,p3 represent pointers point to the first element of first second and third row respectively.

So in effect we can consider array[3] as a pointer and array[1]equivalent to p1 similarly array[2] and 3 Hence to get the value of array[1][2] we can use *(array[1]+2) or ((array+1)+2) or considering it is implemented in row major form *(array+1) is equivalent to array+1*5 and you get
*(array+1*5+2)

1

With the following array:

const int ROWS = 3;
const int COLS = 5;
int arr[ROWS][COLS];

although arr points to the memory, where the first element resides, its type is not int* thus incrementing this pointer won't move you to the second int. So this line:

arr[1][2] = 8;

can be replaced by:

*((int*)arr + 1*COLS + 2) = 8;

or you can use the fact, that this array resides within the continuous block of memory, size of which is equal to ROWS * COLS * sizeof(int), therefore you can simply retrieve the pointer to the first element and use it for this purpose:

int* pFirstEle = &arr[0][0];
*(pFirstEle + 1*COLS + 2) = 8;

But note that this is wrong:

**(arr + 1*COLS + 2) = 8;
LihO
  • 41,190
  • 11
  • 99
  • 167
  • First, the type of `arr` is actually `int[3][5]`; `arr` is _not_ a pointer. And converting it to a pointer, then advancing 1, _will_ point to the second element. The type of the elements is, of course, `int[3]`, and not `int`. – James Kanze Mar 15 '13 at 09:32
  • @JamesKanze: What I meant was that `int[3][5]` behaves like `int (*)[5]` when treated as a pointer and by second element I meant the second integer of course, not second row. Anyway I edited my answer. – LihO Mar 15 '13 at 09:58