answersLogoWhite

0


Best Answer

#include<iostream>

#include<cassert>

int main()

{

char str[] = "The quick brown fox jumped over the lazy dog.";

// Access the 5th element ('q' @ index 4) in 6 different ways:

char a = *(str+4);

char b = *(4+str);

char c = str[4];

char d = 4[str];

char e = *(&str[2]+2);

char f = *(2+&str[2]);

// Point to 7th element and access the 5th element:

char* p = &str[6];

char g = *(p-2);

// Test for equality:

assert(a==b);

assert(b==c);

assert(c==d);

assert(d==e);

assert(e==f);

assert(f==g);

assert(g=='q');

}

Explanation:

There is really just one version, a, but just as x+y == y+x, it is commutative thus we get version b as well. Note that the array name alone always returns a reference to the start of the array. Once we factor out all the pointer arithmetic, we are essentially just returning the dereferenced value of an address within the array.

Version c is the conventional notation using the subscript operator. However the subscript operator is merely sugar-coating (a notational convenience). Behind the Scenes, we're actually executing a.

Version d is an unusual form that is rarely seen but is perfectly valid. Just as str[4] is functionally equivalent to *(str+4), so 4[str] is functionally equivalent to *(4+str), thus d is the same as b (which is the same as a).

Versions e and f are similar to a and b respectively, but instead of taking the address of the start of the array, we now take the address of the 3rd element (index 2) and then offset by 2 elements to get to the 5th element. In these versions, we can also use a negative offset to move backwards from an element, so long as we remain within the bounds of the array. These versions can also be notated more conventionally using version c; str[2+2] which is the same as a.

Version g is similar to e except we now store the address of the 7th element (index 6) in p and then use pointer arithmetic to obtain the offset of the 5th element. However, this version is not commutative so we cannot use *(2-p), but is functionally the same as a.

There are other variants similar to these 7 but they all ultimately come back to version a once you factor out all the pointer arithmetic. In other words, no method is better or worse than any other method. However version c is favoured for its readability alone, regardless of whether the offset is a compile-time constant or needs to be calculated at runtime. All others are academic but give a better understanding of what's really going on behind the scenes.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are all the different methods of accessing array elements?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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

n


What is faster access the element in an array or in a list?

Array is always faster to read from disk/access any element in the array is quicker since elements in a array are stored in contiguous location in the memory, you need the pointer to the head or 0th element in the array and then it much quick to navigate to the next on index based. But you need to know INDEX of the element for best results List (say linked list) will be slower since not always elements are stored in contiguous location in the memory as well it involves a function call which is can be assembler/cpu expensive. However getting an individual object from an array is faster if you know the index of the object. Walking through a linked list is faster than walking through an array, if you use a non-recursive algorithm. --Vinay Solanki


What are Array variables also called as?

Elements of the array.


What is The number of elements in an array is called?

the length of the array


Java program to solve lucky number?

Look at the definition, http://en.wikipedia.org/wiki/Lucky_number , then create an array of integers and start working on it. The sieve method requires you to eliminate certain numbers; you can either replace the eliminated array elements with a marker, or move the non-eliminated elements towards the beginning of the array (overwriting the eliminated elements), or perhaps copy to a different array - depending on whether you want simplicity in programming (putting a marker, such as 0, is probably easier to program), or efficiency.Look at the definition, http://en.wikipedia.org/wiki/Lucky_number , then create an array of integers and start working on it. The sieve method requires you to eliminate certain numbers; you can either replace the eliminated array elements with a marker, or move the non-eliminated elements towards the beginning of the array (overwriting the eliminated elements), or perhaps copy to a different array - depending on whether you want simplicity in programming (putting a marker, such as 0, is probably easier to program), or efficiency.Look at the definition, http://en.wikipedia.org/wiki/Lucky_number , then create an array of integers and start working on it. The sieve method requires you to eliminate certain numbers; you can either replace the eliminated array elements with a marker, or move the non-eliminated elements towards the beginning of the array (overwriting the eliminated elements), or perhaps copy to a different array - depending on whether you want simplicity in programming (putting a marker, such as 0, is probably easier to program), or efficiency.Look at the definition, http://en.wikipedia.org/wiki/Lucky_number , then create an array of integers and start working on it. The sieve method requires you to eliminate certain numbers; you can either replace the eliminated array elements with a marker, or move the non-eliminated elements towards the beginning of the array (overwriting the eliminated elements), or perhaps copy to a different array - depending on whether you want simplicity in programming (putting a marker, such as 0, is probably easier to program), or efficiency.

Related questions

What is the difference between an array and structure?

An array is a collection of related data elements of same type.Structure can have elements of different types.An array is a derived data type.A structure is a programmer-defined data type.A struct can contain multiple data types, whereas an array can not.


What distinguishes an array from a structure?

Am array is an aggregate of elements that must be of the same type. A structure is an aggregate of elements (members) that can be of different types.


When can you use index numbers?

When you are accessing an array's element.


What is array literal in as2?

An array literal is a comma-separated list of the elements of an array. An array literal can be used for initializing the elements of an array.


How can you copy one array in to a different array?

Iterate through all of the elements, and assign them one by one. for (i=0, i&lt;N, i++) a[i] = b[i];


What is difference between structure and array?

An array is a made up of elements of the same type e.g. strings or integers. A structure on the other hand can be made up of elements of different types.


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

n


How do you declare a string array and add elements to it in C plus plus?

You cannot add elements to a fixed array in C or C++. If, however, the array is declared as a pointer to an array, you can add elements by allocating a new array, copying/adding elements as needed, reassigning the new array to the pointer, and deallocating the original array.


What is faster access the element in an array or in a list?

Array is always faster to read from disk/access any element in the array is quicker since elements in a array are stored in contiguous location in the memory, you need the pointer to the head or 0th element in the array and then it much quick to navigate to the next on index based. But you need to know INDEX of the element for best results List (say linked list) will be slower since not always elements are stored in contiguous location in the memory as well it involves a function call which is can be assembler/cpu expensive. However getting an individual object from an array is faster if you know the index of the object. Walking through a linked list is faster than walking through an array, if you use a non-recursive algorithm. --Vinay Solanki


What is a example of a array?

An ordered array is simply an array where all elements are in sorted order: int a[] = {3, 6, 9, 10, 15, 21}; // ordered array An array can either be initialised with ordered elements or the elements may be sorted after initialisation. When inserting new elements into an ordered array, the order must be maintained.


What are Array variables also called as?

Elements of the array.


What is The number of elements in an array is called?

the length of the array