Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a 2D-array in C, without using the operator []?

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!

like image 299
Isan Rivkin Avatar asked Feb 10 '16 10:02

Isan Rivkin


2 Answers

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

*(*(matrix+i)+j)
like image 159
Zbynek Vyskovsky - kvr000 Avatar answered Sep 20 '22 10:09

Zbynek Vyskovsky - kvr000


You can try this-

*(*(matrix+i)+j)  //reduce level of indirection by using *  
like image 36
ameyCU Avatar answered Sep 23 '22 10:09

ameyCU