Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting one-dimensional arrays from a two-dimensional array

Tags:

arrays

c

c89

I have an array like

int outer[4][3] = {
    { 1, 2, 3 },
    { 2, 3, 5 },
    { 1, 4, 9 },
    { 10, 20, 30 }
};

and I would like to get a pointer/array for the n-th one-dimensional array inside outer, something like

void foo () {
    printf("%d %d %d\n", outer[1][0], outer[1][1], outer[1][2]);
    int inner[3] = outer[1]; /* Is there some way to do this assignment? */
    printf("%d %d %d\n", inner[0], inner[1], inner[2]);
    /* so that this line gives the same output as the first */
}

Of course this is possible with pointer math, but I feel like there is some syntax for this that I've forgotten.

like image 831
Charles Avatar asked Feb 02 '16 16:02

Charles


People also ask

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

In this C# Program, we are reading the elements of the 2-Dimensional matrix. Using for loop assign the value of 'a[i,j]' variable to b[] array variable. Increment the value of base index 'k' variable. Print the value of one dimensional array.

How are 2D arrays and 1D arrays related?

Arrays can be created in 1D or 2D. 1D arrays are just one row of values, while 2D arrays contain a grid of values that has several rows/columns. 1D: 2D: An ArrayList is just like a 1D Array except it's length is unbounded and you can add as many elements as you need.

What is the difference between one dimensional and two dimensional array?

Arrays can be single or multidimensional. The number of subscript or index determines the dimensions of the array. An array of one dimension is known as a one-dimensional array or 1-D array, while an array of two dimensions is known as a two-dimensional array or 2-D array.

How to create a multidimensional array in C programming?

In C programming, you can create an array of arrays known as multidimensional array. For example, float x[3][4]; Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can think the array as table with 3 row and each row has 4 column.

Can an array have more than one dimension in Python?

Arrays can have more than one dimension. For example, the following declaration creates a two-dimensional array of four rows and two columns. int ] array = new int[4, 2]; The following declaration creates an array of three dimensions, 4, 2, and 3.

How to create a two-dimensional array in Java?

To create a two dimensional array in Java, you have to specify the data type of items to be stored in the array, followed by two square brackets and the name of the array. Let's look at a code example. Don't worry if you're yet to understand what's going on above.


1 Answers

For pointer to array, declare inner as a pointer to an array of 3 int

int (*inner)[3] = &outer[1]; 


If you want a pointer to first element of array outer[1] then

int *inner = outer[1];  

will do the job. You can also do

int *inner = &outer[1][0];
like image 90
haccks Avatar answered Oct 13 '22 15:10

haccks