answersLogoWhite

0


Best Answer

An array is an aggregate of elements, where memory is allocated to accommodate a given number of elements of a given type. The name of the array serves as a reference to the first element of the array. Unlike ordinary (non-array) variables, all the other elements of an array have no name; they are anonymous. However, all elements of an array have identity (they have an address) so if we know the address of an element within an array we can easily refer to it by that address.

Given that each element is of the same type and therefore the same size (in bytes), we can easily calculate the address of each element offset from the start of the array. That is, the nth element of an array A of type T will be found at address A + sizeof(T) * (n-1). Although we are free to use "pointer arithmetic" like this to calculate the individual addresses of each element, C provides us with a much more convenient notation called the array suffix operator.

The array suffix operator applies to pointer variables only. Fortunately, all arrays implicitly convert to a pointer at the slightest provocation so we don't have to do anything special to use them. The operator is denoted using square brackets [] such that for an array A we can refer to its nth element as A[n-1]. Given that A is of type T, the compiler has enough information to generate the required pointer arithmetic for us: A + sizeof(T) * (n-1).

Note that array indices are in the range 0 to n-1 for an array of n elements. Attempting to access elements outwith this range has undefined behaviour, so it is important that we take steps to ensure all indices are kept within the bounds of the array. For fixed-length arrays, we can simply use a constant to store the array length, but for variable-length arrays we must keep track of the length using a variable. To range-check a given index against a given length, n, the index must be in the closed range [0:n-1]. However, array index ranges are often denoted using half-closed notation, [0:n), which essentially means 0 <= index < n.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is an array and what does index mean in an array in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How can you get position of number from given some number by using array in c language?

Traverse the array from index 0 until you find the number. Return the index of that number.


Why the array is starting from zero?

By design; it makes the compiler's work easier. 1-based array's addressing-function: Address (array, index) = Address (array) + (index-1)*Elemsize(array) 0-based array's addressing-function: Address (array, index) = Address (array) + index*Elemsize (array)


Why array index always starts with zero?

Because that's how the language is defined. In C, C++, and many other languages, array indecies start with zero. This is by convention, and it also reflects the fact that, in C and C++, array-index syntax can also be expressed in pointer-offset syntax. The pointer locates the first element of the array, and the offset indicates how many elements to skip. If a is an array name, then the expression a[n] is completely identical to the expression *(a+n), and vice versa.


What is an unidentified index in php?

Probably it's "Undefined Index".You must have seen it in some of the PHP notices or warnings.It comes, when you are working with arrays and the Index or Key your code is using doesn't actually exist.For Example,if you define an array:$a = array( 'a'=>'pak' , 'b' => 'asif' );having two Indexes (a & b),But later on, you code like:$a['c'];Now, c index doesn't actually existed in $a array, so a PHP warning / notice will appear on run time saying:Undefined Index 'c' ....Hope this help you!


What is the relationship between the value of the subscript and the value of the array element in c?

You can access the array-element via index (or subscript), but it is not possible the other way around.


What happens if an array overrun occurs in java?

If you mean that you try to access an index outside of the bounds of the array, then it will result in an IndexOutOfBoundsException being thrown.


How you use negative index in array?

You cannot do this easily in C programming. Arrays in C always start with index 0. If you must use a negative array index, you can do that by allocating an array, and then pointing to an element within the array. Say you allocate an array 10 ints long, named a; then set b = &amp;a[5]. Then b[-4] = a[1]. However, this is extremely bad programming practice and is almost certain to cause data corruption. Some compilers will treat array indices as unsigned, also, so that when you specify b[-4], the compiler will internally simplify that to b[65532] and your program will crash.


What is the index number of the last element of an array with 9 elements?

(array.length - 1) will find the index of the last element in an array (or -1 if the array is empty).


What is difference between key and index in php arrays?

A key is the name of a variable in an array ($array["key"]) and the index is the position it's at ($array = ["key" =&gt; 0], the index would be 0). Keys and indices are the same if the array is not associative though ($array = [true], the key holding the value true is named 0 and is at index 0).


Is array index out of bound allowed in C language?

No, it is not allowed to exceed the allocated size of an array. However, few compilers check, so if the programmer fails to check, he or she can get in trouble, by corrupting other memory or throwing a bus exception.


What does square brackets mean?

It means indexing into an array. The array could be an array of built in primitive types or array of objects. The index must be a numeric value greater than or equal to 0.


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; }