Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

May I treat a 2D array as a contiguous 1D array?

Consider the following code:

int a[25][80]; a[0][1234] = 56; int* p = &a[0][0]; p[1234] = 56; 

Does the second line invoke undefined behavior? How about the fourth line?

like image 612
fredoverflow Avatar asked Sep 01 '11 10:09

fredoverflow


People also ask

Are 2D arrays contiguous?

As a result, the element locations within a row are contiguous, but elements are not contiguous across rows of the 2D array.

Can it be considered as 1D array or 2D array?

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.

How do you represent a 2D array in one direction?

You should be able to access the 2d array with a simple pointer in place. The array[x][y] will be arranged in the pointer as p[0x * width + 0y][0x * width + 1y]... [0x * width + n-1y][1x * width + 0y] etc.

Are 2D arrays contiguous in C?

Are 2D arrays contiguous in C? - Quora Are 2D arrays contiguous in C? Yes. At declaration the total size of the array is calculated by the compiler and reserved, then the variable named for the array is assigned to point to the first element of the array’s reserved/assigned memory space (basically, a pointer to the data type of the array elements).

What is the difference between 1D and 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. 1. Krishna, Appili Vamsi. “Arrays 1D and 2D, and Multi-Dimensional.”

How many types of arrays?

An array allows storing multiple items of the same data type. The elements in the array are in subsequent memory locations. There are two types of arrays as one dimensional (1D) array and two dimensional (multi-dimensional) arrays. 1. What is 1D Array 2. What is 2D Array 3. What is the Difference Between 1D and 2D Array

Is it possible to layout a multidimensional array of arrays?

It's up to interpretation. While the contiguity requirements of arrays don't leave much to the imagination in terms of how to layout a multidimensional arrays (this has been pointed out before), notice that when you're doing p [1234] you're indexing the 1234th element of the zeroth row of only 80 columns.


1 Answers

It's up to interpretation. While the contiguity requirements of arrays don't leave much to the imagination in terms of how to layout a multidimensional arrays (this has been pointed out before), notice that when you're doing p[1234] you're indexing the 1234th element of the zeroth row of only 80 columns. Some interpret the only valid indices to be 0..79 (&p[80] being a special case).

Information from the C FAQ which is the collected wisdom of Usenet on matters relevant to C. (I do not think C and C++ differ on that matter and that this is very much relevant.)

like image 163
Luc Danton Avatar answered Sep 28 '22 03:09

Luc Danton