answersLogoWhite

0


Best Answer

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
This answer is:
User Avatar

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

Related questions

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.


Program in c to delete an element in sorted array?

You cannot delete elements from an array. But you can move the elements: if (del_index &lt; no_of_elements-1) { memmove (&amp;array [del_index], &amp;array [del_index+1], sizeof (array [0]) * (no_of_elements - del_index - 1)); } --no_of_elements;


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


When an array name is passed to function in c?

It 's address is received by the function . that any changes in the value of array elements in the function will result in actual change.


How can you merge the contents of two sorted arrays into a new array such that the contents of this new array is sorted?

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.


C program to copy one matrix to another matrix?

#include main() { int array[100], minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] < minimum ) { minimum = array[c]; location = c+1; } } printf("Minimum element is present at location number %d and it's value is %d.\n", location, minimum); return 0; }


How arrays must be handled in c plus plus?

If the array is static it can declared in the structure itself: struct myArrayTag { int num[12]; // array of 12 integers (e.g., 48 bytes). } myArray; If it is dynamic then you must use a pointer and allocate the array outside the structure. You should also maintain a variable in the structure to keep track of how many elements the array currently has: struct myBufferTag { int * array; // Pointer to array of integers. int size; // Size of array (number of elements); } myBuffer;


When an array is declared does c automatically initializes its elements to zero?

For global/static variables: yes.For auto variables: no.