C Tutorial Advanced C arrays
Advanced C Arrays

Plus Syntax
Pointer Style and strcpy()
Pointer Type Effects
Arrays and Pointers
Array Names Are Const
Heap Memory
Memory Management
Dynamic Arrays
Advantages of being in the heap
Disadvantages of being in the heap
Dynamic Strings
 
 

 

HOME
 

Advanced C Arrays

In C, an array is formed by laying out all the elements contiguously in memory. The

square bracket syntax can be used to refer to the elements in the array. The array as a

whole is referred to by the address of the first element which is also known as the "base

address" of the whole array.

 

{

int array[6];

 

int sum = 0;

sum += array[0] + array[1];   // refer to elements using []

}

 

The programmer can refer to elements in the array with the simple [ ] syntax such as

array[1]. This scheme works by combining the base address of the whole array with the index to compute the base address of the desired element in the array. It just requires

a little arithmetic. Each element takes up a fixed number of bytes which is known at compile-time. So the address of element n in the array using 0 based indexing will be at

an offset of (n * element_size) bytes from the base address of the whole array.

 

address of nth element = address_of_0th_element + (n * element_size_in_bytes)

 

The square bracket syntax [ ] deals with this address arithmetic for you, but it's useful to know what it's doing. The [ ] takes the integer index, multiplies by the element size, adds the resulting offset to the array base address, and finally dereferences the resulting pointer

to get to the desired element.

 

{

int intArray[6];

 

intArray[3] = 13;

}

Want To Know more with

Video ???

Contact for more learning: webmaster@freehost7com