answersLogoWhite

0

Individual elements are identified by address. Unlike ordinary variables, we can't identify them by name because array elements are anonymous; they have no name. However, we always know the start address of the array (which can be named) and every element in the array can be referred to by its zero-based offset from this address. That is, the first element is offset 0 elements from the start address because there are zero elements before it. By the same token the nth element is offset n-1 elements from the start address because there are n-1 elements in front of it.

int a[100];

int* p1 = a + 0; // refers to the 1st element (1-1=0)

int* p2 = a + 5; // refers to the 6th element (6-1=5)

int* p3 = a + 99; // refers to the 100th element (100-1=99), the last element

int* p4 = a + 100; // refers to one-past-the-end of the array

Note that p4 is valid because that address is guaranteed to exist even though it is physically beyond the bounds of the array. Although valid, we must never dereference that address because it is not guaranteed to belong to our program. However, we often use the one-past-the-end of a sequence in algorithms that use the end iterator to denote the end of a half-closed range denoted [begin:end). For instance, the following algorithm provides the most efficient means of linearly searching for a given value within any given contiguous sequence of an array, returning the end iterator if the value does not exist:

int* find (int* begin, int* end, int value) {

// search every element to find the value, unless we reach the end iterator

while (begin!=end && *begin!=value) ++begin;

return begin;

}

// search for the value 42 within the whole array

int* i1 = find (a, a+100, 42);

if (i1 == a+100) {

// the value was not found

} else {

// the value was found

}

int*_t i2 = find (a, a+10, 42); // search for the value 42 within the first 10 elements only...

They array suffix operator [] provides a more intuitive means of referring to the individual elements of an array without resorting to pointer arithmetic:

// initialise the array referred to by a of length s elements with the value v

void initialise_all (int* a, size_t s, int v)

{

for (size_t idx=0; idx<size; ++idx) // idx is the zero-based index

{

a[idx] = value;

}

}

initialise (a, 100, 42); // assign the value 42 to all elements of a

Note that the pointer arithmetic still occurs, it's just hidden from the programmer. The array suffix operator is merely sugar-coating. As far as the compiler is concerned, the above is equivalent to the following:

// initialise the array referred to by a of length s elements with the value v void initialise_all (int* a, size_t s, int v)

{

for (size_t idx=0; idx<size; ++idx) // idx is the zero-based index

{

*(a+idx) = value; // pointer arithmetic

}

}

A good compiler will translate the above into the more succinct and highly efficient version:

// initialise the array referred to by a of length s elements with the value v

void initialise_all (int* begin, int* end, int val)

{

while (begin!=end) *(begin++) = val;

}

Although more difficult to read, explicitly using pointer arithmetic yields efficient code regardless of a compiler's ability to optimise your code Behind the Scenes. However, with modern compilers, there should be no difference to the resultant code.

User Avatar

Wiki User

7y ago

Still curious? Ask our experts.

Chat with our AI personalities

ReneRene
Change my mind. I dare you.
Chat with Rene
JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach

Add your answer:

Earn +20 pts
Q: How individual elements are identified of an array in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you accept values of array of structure from user in c?

How do you accept total no of array elements and values from the user in c?


Can you help me with the C plus plus code of the program which has 10 index of array it adds 5 into every even elements of the array and then it subtracts 10 into the odd elements of the array?

int array[10] = {...}; for (int i = 0; i &lt; 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }


Difference between integer array and character array?

Arrays are basic structures wherein one or more elements exist "side by side" and are of the same "type". An "integer" array is an array whose elements are all of an integer type which has no fractional component. A "character" array is an array which contains nothing but character types. A "floating point" array contains elements that have both an integer and fractional portion. Simply put, they are arrays of particular types.


How do you create an element in an array using C plus plus program?

If this is a homework related question, you really should consider trying to solve it yourself before looking at this answer. Otherwise, the value of the lesson, and the reinforcement provided by the assignment, will be lost to you. You do not create individual elements of arrays in C or C++. You create the array, and then reference the elements. Say you want to create a 10 x 20 array of floats. You would declare float MyFloatArray[10][20]. You would then reference the elements, such as MyFloatArray[3][8]. Note that 10 and 20 are the number of elements in each direction, but the valid range of indices is 0 to 9 and 0 to 19, respectively.


Write a C programme to find out sum of the array elements?

main(){int n,a[i],s;s=0;printf("enter no of elements in array");scanf("%d",&n);printf("Enter elements in array");for(i=;i