Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a one-dimensional array as a two-dimensional array

I have a one-dimensional array int *data and I know the number of rows and cols which are effectively stored within it. That is, I can access an element of the two-dimensional matrix (i,j) by using something like data[i*cols+j].

Specifically, these are stored in a struct:

typedef struct {
    int *data;
    int rows;
    int cols;
} matrix_t;

At some point in the past, I also wrote this voodoo code:

#define MATRIXP(m) __typeof__(__typeof__(__typeof__(*((m).data))[(m).cols]) *)
#define MATRIXD(m) ((MATRIXP(m)) ((m).data))

With these definitions, I am able to do:

MATRIXP(matrix) m = MATRIXD(matrix);

And then I can use the matrix m as a two-dimensional matrix pointer for accessing data.

m[3][2] = 5; /* assign the element in row 3 and column 2 */

This is very nice and means I don't need to remember to always have an expression like data[i*cols+j]. However, I wrote this code some time ago and I now cannot remember how it works.

  1. Can someone please explain exactly how all those __typeof__ operators are working, and how to go about reading similar expressions? What is the type of the variable m?

    I know it expands to something like:

    __typeof__(__typeof__(__typeof__(*((matrix).data))[(matrix).cols]) *) m = ((__typeof__(__typeof__(__typeof__(*((matrix).data))[(matrix).cols]) *)) ((matrix).data));
    
  2. Is this method of accessing data safe? Is it the best way to do this?

like image 684
codebeard Avatar asked Apr 30 '14 07:04

codebeard


People also ask

How do you convert a one-dimensional array into two dimensions?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.

How do you convert a one-dimensional array to a two-dimensional array in C?

So the total number of elements of 1D array = (​ m * n ​ ) elements. Call the function​ ​ input_array​ to store elements in 1D array. Call the function ​ print_array​ to print the elements of 1D array. Call the function ​ array_to_matrix​ to convert 1D array to 2D array.

Can we compare 1D array with 2D array?

The main difference between 1D and 2D array is that the 1D array represents multiple data items as a list while 2D array represents multiple data items as a table consisting of rows and columns. A variable is a memory location to store data of a specific type.

How do you access one-dimensional array?

In one-dimensional arrays in C, elements are accessed by specifying the array name and the index value within the square brackets. Array indexing starts from 0 and ends with size-1.


1 Answers

Question 1:

MATRIXP(matrix) m (matrix should be a variable of type matrix_t) will be expanded to

__typeof__(__typeof__(__typeof__(*((matrix).data))[(matrix).cols]) *) m

From in side to out side

  1. __typeof__(*((matrix).data)) is the type of *((matrix).data), which is int according to the definition of matrix_t.

  2. so (__typeof__(*((matrix).data))[(matrix).cols]) equals (int [cols])

  3. so __typeof__(__typeof__(*((matrix).data))[(matrix).cols]) *) equals (int [cols] *)

  4. and that is the type given by MATRIXP(matrix).

Therefore, MATRIXP(matrix) equals (int [(matrix).cols] *). In other words,

MATRIXP(matrix) m

is effectively

int (*m)[matrix.cols]

As pointed out by @codebeard

Question 2:

It looks quite safe to me.

like image 135
Lee Duhem Avatar answered Oct 07 '22 01:10

Lee Duhem