UnsettledRoell
UnsettledRoell

Reputation: 1

How do I point to multi-dimensional array values?

I need some help on multi-dimensional arrays... I cannot find out how to assign a value to an array in a void task, that was created in main. i tried to find help all over the place, but the longer i keep reading the less i understand

Please help

void addValue(a,b)
{
  //somehow assign value to a[2][6] using pointers and such

void main()
{
  int dest[7][7] = { 0 };
  int a = 2;
  int b = 6;
  addValue(a,b);
}

Upvotes: 0

Views: 56

Answers (1)

M.M
M.M

Reputation: 141554

Like this:

void addValue( int (*dest)[7], int a, int b )
{
    dest[2][6] = 12;
}

int main()     
{
    int dest[7][7] = { 0 };
    addValue(dest, a, b);
}

Consider using std::array instead of C-style arrays; the latter are an anachronism in C++. You can make the 7 a template parameter in addValue if you want to support other dimensions of array.

Upvotes: 1

Related Questions