guys, we use like a 2-d array very often.
But what exactly is the use of a 2-d array ?

Now when i look at it, i feel everything can be done using a 1-d array and incrementing the pointer appropriately.
Is the only advantage of a 2-d array , matrix representation ? (By the way, matrices are very important, i understand cuz its matrix multiplication that happens in GPU's)

But can someone give me another example, or induce a different kind of thought into this.

Thanks.

Now when i look at it, i feel everything can be done using a 1-d array and incrementing the pointer appropriately.

Just because you can do something doesn't mean it's always the best way.

Just because you can do something doesn't mean it's always the best way.

@Narue: So can u give an example where you compulsarily have to use a 2-d array.

There's no such situation. You can always simulate the effect of a 2D array without actually using one. At this point, I have no idea what you're looking for aside from imaginary guarantees that save you from making an informed decision in your own code.

There's no such situation. You can always simulate the effect of a 2D array without actually using one. At this point, I have no idea what you're looking for aside from imaginary guarantees that save you from making an informed decision in your own code.

I have a couple of doubts and trying to solve these gave rise to those.
1)Why is the syntax for function definition like this ?

int fun_name(int array[])
{ 

}

You are passing the address of the first character of the array. How is that stored in int array[] ?


2)If you can receive a 1-d array like mentioned above(in 1), then why cant you receive a 2-d array as

int fun_name(int array[][])
{

}

I have found in text-books and resources that this is rather the syntax
int fun_name(int array[])

{

}

My argument : All that you need to access any element of an array is just its starting address cuz array is contiguous memory allocation. So y shud u pass the size then ?

1) Array syntax for the first dimension is sugar for a pointer. These two are equivalent:

int fun_name(int array[])
int fun_name(int *array)

So array isn't actually an array, it's a pointer. Personally, I think arrays and pointers would be somewhat less confusing if this particular feature didn't exist.

2) Because only the first dimension enjoys the conversion to a pointer. The syntax falls out of the pointer conversion of an array name used in value context. Passing a bare array name as an argument is value context, so the conversion for this:

int fun_name(int array[][20]);
int array[10][20];

fun_name(array);

is conceptually like so:

int fun_name(int array[][20]);
int array[10][20];
int (*__temp)[20] = &array[0];

fun_name(__temp);

The usefulness of applying that conversion recursively to all dimensions is dubious at best.

So y shud u pass the size then ?

That's easy: because you can't not pass the size. This is a syntactical rule of C. Whether you agree with the decision or not, it's set in stone at this point.

I used a 2 d array of characters to make the board of my four in a row program

made a state struct that had different fields including a brd field which was a board a 2d array then that had the configuration of every move in it and stored each state in an N-ary tree so that my heuristic function could make a good calculation of what the user's move was going to be

This article has been dead for over six months. Start a new discussion instead.