Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C weird array syntax in multi-dimensional arrays

I've known that this is true:

x[4] == 4[x]

What is the equivalent for multi-dimensional arrays? Is the following true?

x[4][3] == 3[x[4]] == 3[4[x]]
like image 490
ypercubeᵀᴹ Avatar asked Nov 13 '11 02:11

ypercubeᵀᴹ


People also ask

What is an array write the syntax for multi-dimensional array in C?

Examples: Two dimensional array: int two_d[10][20]; Three dimensional array: int three_d[10][20][30]; Size of Multidimensional Arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions.

What is multi-dimensional array in C explain with example?

A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array, or two-dimensional array, is an array of arrays, meaning it is a matrix of rows and columns (think of a table). A 3D array adds another dimension, turning it into an array of arrays of arrays.

Does C support multi-dimensional array?

In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float x[3][4];


1 Answers

x[y] is defined as *(x + (y))

x[y][z] would become *(*(x + (y)) + z)

x[y[z]] would become *(x + (*(y + (z))))


x[4][3] would become *(*(x + (4)) + 3) would become *(*(x + 4) + 3)

3[x[4]] would become *(3 + (*(x + (4)))) would become *(*(x + 4) + 3)

3[4[x]] would become *(3 + (*(4 + (x)))) would become *(*(x + 4) + 3)

Which means they are all equivalent.

like image 138
Pubby Avatar answered Oct 15 '22 19:10

Pubby