answersLogoWhite

0


Best Answer

element_address = array_address + (element_index * element_size)

Note that the array name refers to the start address of the array, element indices are always zero-based and element size is calculated at compile time using the sizeof() operator using the array type as the operand.

int x[10];

int* p1 = x + (5 * sizeof(int)); // calculate address of 6th element

int* p2 = &x[5]; // return address of 6th element

assert (p1==p2); // ensure both address are equal

Note that the array suffix operator [] used by p2 is merely sugar-coating. Behind the Scenes, the compiler will automatically generate code equivalent to that of the p1 calculation. As such, there is no advantage in using one over the other.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you calculate address of any element of array using formulae?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is offset address?

An offset address is a relative address rather than an absolute address. You use offsets to refer to memory relative to an absolute address. For instance, array indices are implemented using offsets from the start address of the array, such that element 0 is at offset 0 and element 5 is at offset 5.


Why and is not required for an array in scanf in c?

The name of the array means the address of the first element, so 'arr==&arr[0]'


How do you draw pascals triangle in gwbasic?

I suggest using an array with as many elements as the longest row you need. To keep it simple, keep two copies of the array, and calculate each element of the "new" array as the sum of the corresponding element, plus the previous element, of the "old" array. Then copy the information back for the next step.


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.


What is traversing arrays?

Traversing an array simply means visiting every element of the array exactly once. Pointers facilitates this job. A pointer is first created to contain the base address of the array thereby pointing the first element. As the pointer is incremented, it points to the very next element and so on till the last element is visited. Thus the elements are visited sequentially in the same order as they are stored in the array.

Related questions

Which formula you will use to calculate The memory address of fifth element of an array?

a


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


Which formula you will to calculate The memory address of fifth element of an array?

Awais khanLOC(LA[K]) =base(LA)+w(k-lowerbound)


What is the memory address of some element if the base address is x and each element of the array takes e memory locations?

The memory address of some element if the base address is x, each element of the array takes e memory locations, and the index based zero of the element is i, is x + ie.


How to display the content of memory address stored in an element of an array?

void *array[2]; printf ("array[%d]=%p\n", i, array[i]);


How do you calculate array elements value?

You don't need to calculate an array element's value. An array element is a variable and like any variable you can access its value directly. There is nothing to calculate: int a[5] {0, 2, 4, 6, 8}; int x = a[2]; // x=4 The only thing that really needs calculating is the index of the element you wish to access. If you know the index, then there's nothing to calculate.


Is it possible to pass a portion of an array to a function?

Yes. Since passing arrays is a special use of call by reference, simply pass the address of the sub array instead of the primary array. int a[10] = { ... }; myfunction (a); // pass the first element's address myfunction (&(a[3]); // pass the fourth element's address


What is offset address?

An offset address is a relative address rather than an absolute address. You use offsets to refer to memory relative to an absolute address. For instance, array indices are implemented using offsets from the start address of the array, such that element 0 is at offset 0 and element 5 is at offset 5.


What is the use of Arrays?

Arrays are a primitive container for data of the same type. The elements of an array are stored in a block of contiguous memory, one after the other. As such, an array offers the most compact method of storing a collection of values. Unlike ordinary variables, which can be named, the elements of an array are anonymous; we can only refer to them by their memory address. However, given that each element is the same type and therefore the same length (in bytes), it is trivial to calculate the address of any element within the array knowing only the start address of the array (which can be named) and the zero-based index of the element we wish to access, such that the first element resides at index 0. Since calculating individual addresses is a constant-time operation, this makes it possible to perform constant-time random-access to any element in the array. However, the array suffix operator means we do not need to manually calculate individual addresses, we need only know the zero-based index of the element we wish to access.


Why and is not required for an array in scanf in c?

The name of the array means the address of the first element, so 'arr==&arr[0]'


How do you draw pascals triangle in gwbasic?

I suggest using an array with as many elements as the longest row you need. To keep it simple, keep two copies of the array, and calculate each element of the "new" array as the sum of the corresponding element, plus the previous element, of the "old" array. Then copy the information back for the next step.


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.