5

I am trying to print a 2D matrix with using [], instead I want to use * like a pointer.
So with a 1 D array I'd do: *(arr+i) for example. What's the syntax used to replace in matrix[][] ?

Here's the code:

for (i = 0; i < size; i++)
{
    for (j = 0; j < (size * 2); j++)
    {
        printf(" %5d", matrix[i][j]);
    }
    printf("\n");
}

P.S, I did try several things like:

*(matrix+i+j);
*(matrix+i)+*(matrix+j);

Of course none of that worked.

Thank you for your help and time!

3
  • Is matrix T[][] or T** ? Commented Feb 10, 2016 at 10:40
  • I recommend that you use pointer syntax only for learning purposes. Array syntax should be preferred, as it's simpler and easier to understand. Some people think pointer syntax is more effective, but in truth they are both equally fast. Commented Feb 10, 2016 at 10:44
  • Good point, I am actually just doing an exercise form studies :) Commented Feb 10, 2016 at 10:47

3 Answers 3

6

Use the * two times. Each of * will basically replace one []:

*(*(matrix+i)+j)
Sign up to request clarification or add additional context in comments.

17 Comments

@Pixelchemist No it's not. It works due to array decaying into pointer.
@user694733 Why would array decay to pointer here ? ( if not previously declared a pointer).
@ameyCU user694733 is correct. Rhetorical question: how can you have pointer arithmetic without pointers?
Array matrix with type int[][] decays to pointer &matrix[0] with type int(*)[] (I hope I wrote that right) before +i is performed.
N1570 chapter 6.5.2.1 has example of subscripting array int x[3][5]; with sentence: "...In the expression x[i], which is equivalent to (*((x)+(i))), x is first converted to a pointer to the initial array of five ints. ..."
|
2

You can try this-

*(*(matrix+i)+j)  //reduce level of indirection by using *  

Comments

0

This may depend on how matrix was allocated or passed to a function.

int A[10][15];

This uses a contiguous block of memory. To address the elements without using array notation use:

        A +(i*15)+j    // user694733 showed this is wrong
((int *)A)+(i*15)+j    // this is horribly ugly but is correct

Note the 15, as each row consists of 15 elements. Better solutions are presented in other answers here.

In the following:

int *A[10];

A is an array of 10 pointers to ints. Assuming each array element has been allocated using malloc, you address the elements without using array notation as:

*(A+i) + j;

that is, you take A, then take the ith element, dereference that and add j as the second index.

--EDIT--

And to be complete:

int foo(int *p)

here a function just receives a pointer to zero or more ints. The pointer points to a contiguous, linear block of memory into which you can place an n-dimensional array. How many dimensions there are and the upper bound of each dimension the function can only know through parameters or global variables.

To address the cells of the n-dimensional array, the programmer must calculate the addresses him/herself, using the above notation.

int foo3(int *m, int dim2, int dim3, int i,  int j, int k)
{
    int *cell = m + i*dim3*dim2 + j*dim2 + k;
    return *cell;
}

8 Comments

Using A+(i*15)+j is error prone and unnecessary. It's only needed if you allocate 2D as 1D array.
@user694733, all arrays of the form int A[10][15] are contiguous in memory. That has nothing to do with "allocating a 2D as 1D array." I can treat any contiguous block of memory as an n-dimensional array. It is just to show the OP how to do it. It is not nice, but that's what he wants to learn.
*(*(matrix+i)+j) does it without needing to specify the size explicitly.
Your "true" 2D array, defined as int A[10][15]" is just a contiguous block of memory and the compiler uses the A+(i*N)+j calculation.
Perhaps it does so internally. But if you do it manually, then types are all wrong, unless you add casting. For variable int m[2][2]; accessing last element m[1][1] using syntax m+(i*size)+j, now m+(1*2)+1, will expand to (char*)&m[0] + sizeof(int[2]) * ((1*2)+1), which is out of array bounds. Example on Ideone.
|

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.