answersLogoWhite

0


Best Answer

If the array is a local fixed-length array, use the address-of operator (&).

Example:

int x[3][4]; // local fixed-length array

int* p = &x[2][1]; // obtain the address of element x[2][1]

//...

For variable-length arrays we need to use a pointer and separate variables to keep track of the dimensions. C uses row-major semantics so the first dimension specifies the number of rows. However, the type of element in each row is not int, it is int[4], as per the second dimension. In other words, it is one-dimensional array of 3 one-dimensional arrays each of length 4. However, we cannot have a pointer of type int[4] because int[4] also decays to a pointer. We could use a pointer to pointer to int (int**), however in order to allocate the array contiguously without having to maintain a separate array of pointers into the second dimension, it is simpler to treat the array as if it were one-dimensional using a pointer to int (int*). After all, the actual elements we wish to access are integers, so that's what we should really be referring to, rather than to rows of arrays of integers which adds an unnecessary level of indirection. This then means we must use pointer-arithmetic to determine the address of each element. The following demonstrates how to use pointer arithmetic to access each element of a two-dimensional array through a pointer given only the row and column of the element we wish to access:

const size_t rows = 3;

const size_t cols = 4;

int* array2d = malloc (rows * columns * sizeof (int)); // allocate memory to the array

for (int row=0; row<rows; ++row) {

for (int col=0; col<cols; ++col) {

int* p = array2d + (row * cols) + col;

// ...

}

}

free (array2d); // release allocated memory

Here, row * cols determines the offset address of the start of the given row. array2d refers to the start address, so adding array2d gives us the actual address of the row. Adding col then gives us the address of the element within the given row. Note that array2d, &array2d and &array2d[0] are equivalent; they all refer to the start address of the array.

The same technique can also be used when passing a two-dimensional array to a function:

void f (int* array2d, const size_t rows, const size_t cols) {

for (size_t row=0; row<rows; ++row) {

for (size_t col=0; col<cols; ++col) {

int* p = array2d + (row * cols) + col;

// ...

}

}

}

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

2D arrays in C and C++ are like Excel spreadsheets. The data contained in a specific cell is found using its row and column.

For example:

int table[3][3];

for (int i = 0; i < 3; i++)

{

for (int j = 0; j < 3; j++)

{

table[i][j] = (rand()+time(0))%10;

}

}

This code will create a 3x3 2D array named "table" with random 0-9 numbers in each spot.

Back to the question using this example:

To pull information from one specific position, you would use:

table[i][j]

where 'i' and 'j' can be switched out for a specific number or variable

The question was posted in C++, so you may need to modify the code for any non-C compatible stuff from my C++ code.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you determine the address of a two dimensional array element in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is meant by single array?

A single dimensional array, or one-dimensional array, is a contiguous block of memory that contains one or more elements, each of which can store a single fixed-width value. Since each element is the same length (in bytes), the total memory consumed by the array is equal to the product of the element length and the total number of elements.One-dimensional arrays can be thought of as being a single row or column of numbered pigeon holes (the elements), such that each hole may contain a single object (a value). The numbers identifying each element are a zero-based incremental subscript, or index number. Thus for an array of n elements, the indexes will be in the range 0 to n-1. Some languages permit the first index to be offset from 0 such that the indexes will be in the range offset to offset+n-1, however this does not alter the actual index which is always zero-based.Arrays permit random, constant-time access to any element in the array via the element's index. When accessing an element, the return value is a reference to the first memory address of that element. This is achieved through simple pointer arithmetic, such that element i will be found at memory address (i-1) x (size of element) bytes from the start address of the array (hence the index is always zero-based, regardless of any offset that is applied).The value's stored in the elements must be of the same intrinsic type, but can be variables or constants of any kind, including object references and pointer variables, even pointers to other arrays.Multi-dimensional arrays are merely an extension of one-dimensional arrays, such that each element contains a reference to another one-dimensional array (each element of which may refer to yet another one-dimensional array, and so on). For instance, a two-dimensional array can be thought of as being pigeon holes arranged in rows and columns, such that every row has the same number of columns, and every column has the same number of rows. A three-dimensional array can be thought of as being a cuboid of pigeon holes. Although it's difficult to imagine a four-dimensional array in a three-dimensional space, the easiest way to think of it is as being a one-dimensional array where each element contains a three-dimensional array. More simply, a series of cuboids arranged in a row or a column.Multi-dimensional arrays needn't reside in contiguous memory, however each individual one-dimensional array that makes up a multi-dimensional array must itself reside in contiguous memory.Accessing the individual elements of a multi-dimensional array requires one subscript per dimension. Thus for a cuboid that has 3 rows, 3 columns and 3 tiers, the middle box will be found at zero-based index (1, 1, 1). Again, simple pointer arithmetic is employed to determine the address of the element offset from the starting address of each one-dimensional array.


What is the time complexity of accessing an element of an N dimensional array?

n


Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


Is the base address of a two dimensional array the address of the first component true or false?

yes


How are elements of an array stored in memory?

Computer memory is linear so a one dimensional array can be mapped on to the memory cells in rather straight forward manner.To find the actual address of an element one needs to subtract one from the position of the desired entry and then add the result to the address of the the first cell in the sequence.Having said that therefore it is necessary to know the starting address of the space allocated to the array and the size of the each element, which is same for all the elements of an array.The the location of the Ith element would be B+I*S where B is the base address(Starting address of the array) and S is the size of each element of the array.

Related questions

What is meant by single array?

A single dimensional array, or one-dimensional array, is a contiguous block of memory that contains one or more elements, each of which can store a single fixed-width value. Since each element is the same length (in bytes), the total memory consumed by the array is equal to the product of the element length and the total number of elements.One-dimensional arrays can be thought of as being a single row or column of numbered pigeon holes (the elements), such that each hole may contain a single object (a value). The numbers identifying each element are a zero-based incremental subscript, or index number. Thus for an array of n elements, the indexes will be in the range 0 to n-1. Some languages permit the first index to be offset from 0 such that the indexes will be in the range offset to offset+n-1, however this does not alter the actual index which is always zero-based.Arrays permit random, constant-time access to any element in the array via the element's index. When accessing an element, the return value is a reference to the first memory address of that element. This is achieved through simple pointer arithmetic, such that element i will be found at memory address (i-1) x (size of element) bytes from the start address of the array (hence the index is always zero-based, regardless of any offset that is applied).The value's stored in the elements must be of the same intrinsic type, but can be variables or constants of any kind, including object references and pointer variables, even pointers to other arrays.Multi-dimensional arrays are merely an extension of one-dimensional arrays, such that each element contains a reference to another one-dimensional array (each element of which may refer to yet another one-dimensional array, and so on). For instance, a two-dimensional array can be thought of as being pigeon holes arranged in rows and columns, such that every row has the same number of columns, and every column has the same number of rows. A three-dimensional array can be thought of as being a cuboid of pigeon holes. Although it's difficult to imagine a four-dimensional array in a three-dimensional space, the easiest way to think of it is as being a one-dimensional array where each element contains a three-dimensional array. More simply, a series of cuboids arranged in a row or a column.Multi-dimensional arrays needn't reside in contiguous memory, however each individual one-dimensional array that makes up a multi-dimensional array must itself reside in contiguous memory.Accessing the individual elements of a multi-dimensional array requires one subscript per dimension. Thus for a cuboid that has 3 rows, 3 columns and 3 tiers, the middle box will be found at zero-based index (1, 1, 1). Again, simple pointer arithmetic is employed to determine the address of the element offset from the starting address of each one-dimensional array.


Why does array index begins with 0 not with 1?

It is because array name implies its address and if you want to access first element of it pointer address logic is as below: Arrays first element address = array base address + 0 Arrays second element address = array base address + 1


What is a multi array?

A two-dimensional array is the simplest multi-dimensional array and is implemented as a one-dimensional array where every element is itself a one-dimensional array. We can imagine a two-dimensional array as being a table of rows and columns where every row is an array in its own right. A three-dimensional array is simply a one-dimensional array of two-dimensional arrays, which can be imagined as being an array of tables. Extending the concept, a four-dimensional array is a table of tables. Multi-dimensional arrays may be jagged. That is, a two-dimensional array may have rows of unequal length. Unlike regular arrays, jagged arrays cannot be allocated in contiguous memory. Instead, we use the outer array (the first dimension) to store pointers to the inner arrays. An array of strings (character arrays) is an example of a two-dimensional jagged array.


What is the time complexity of accessing an element of an N dimensional array?

n


Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


How 3d arrays are represented in memory?

All multi-dimensional arrays are represented as arrays of arrays. That is, each element of the array is itself an array. Thus a two-dimensional array can be thought of as being a one-dimensional array where every element is a one-dimensional array. A three-dimensional array is therefore a one-dimensional array of two-dimensional arrays. And so on. The actual memory layout of a multi-dimensional array is no different to that of a one-dimensional array of the same size: int a[12]; int b[3][4]; Assuming a 4-byte int, the amount of memory allocated to a is 12 x 4 = 48 bytes. The array b is essentially an array where each element holds an array of 4 integers, thus each element is 16 bytes in length. Given there are 3 such elements in total, the total size of b is 3 x 16 = 48 bytes, the same as was allocated to a. Although the allocations are exactly the same in terms of size, the layouts differ. The array a is an array of twelve 4-byte elements (of type int) whereas array b is an array of three 16-byte elements, each of which is itself an array of four 4-byte elements (of type int). This changes the way we refer to the individual elements of the array. Every element in an array is referred to by its offset address from the start of the array. This is determined by multiplying its zero-based index by the element size. In the case of a, every element is 4-bytes in length, thus element a[2] refers to the element that is offset 2 x 4 = 8 bytes from the start of the array. But in the case of b, however, b[2] would refer to the 16-byte element that is offset 2 x 16 = 32 bytes from the start of the array. Given that we're actually referring to an element which is itself a 4-element array, we must use a second subscript to refer to the elements of that array. Thus b[2][3] would refer to the integer that is offset 3 x 4 bytes from the start of the array referred to by b[2]. Extending this idea into three-dimensions is simply a matter of taking a third subscript into account.


Is the base address of a two-dimensional array the address of the first component true or false?

True.


Is the base address of a two dimensional array the address of the first component true or false?

yes


What an array?

An array is an aggregate of data elements of the same type. Arrays are allocated in contiguous memory. An element of an array can be another array type, also known as a multi-dimensional array.


How are elements of an array stored in memory?

Computer memory is linear so a one dimensional array can be mapped on to the memory cells in rather straight forward manner.To find the actual address of an element one needs to subtract one from the position of the desired entry and then add the result to the address of the the first cell in the sequence.Having said that therefore it is necessary to know the starting address of the space allocated to the array and the size of the each element, which is same for all the elements of an array.The the location of the Ith element would be B+I*S where B is the base address(Starting address of the array) and S is the size of each element of the array.


How are three-dimensional arrays represented in memory Explain how address of an element is calculated in three dimensional array?

3-D arrays can be represented as a single dimension of tables. Each table has rows and columns. Each table may also refered as Page. Let a[x][y][z] is an element of a three dimensional array 'a' at the xth Page, Within that page yth row and zth column. In memory it will be stored as sequence of memory locations. Suppose array index starts from 0,0,0. If the first element of the array is stored in location M, The address of the a[i][j][k] = (i-1)U2U3 + (j-1)U3 + (k-1), Where U2 and U3 are the dimention of a table.


Dope vector method for one dimentional array?

Dope Vector Method is use for one dimensional array and also two-dimensional array for one dimensional array we use MA(i)=sa+(i-1)*w MA is Memory Address Sa = Start Address i is subscript W for integer w=2 float=4 char=1 for two dimensional array MA(i,j)=SA{n(i-1)+(j-1)}*W