Chp 8 Arrays Flashcards
What are arrays?
An array is a collective name given to a group of ‘similar quantities.’
Aka Subscripted variable.
Rules of Array: how are elementws stored? Indexing? Declaring? etc
- All member elements must be of same type.
- All array elements are numbered starting from 0.
- Hence last element is size-1
- Bfore using array its type and dimension must be declared.
- Elements of array are always stored in contiguous (adjacent) memory locations.
Array Declaration:
int arrayName[i] ;
i : A number, which is the size of the Array.
i aka DIMENSION
int specifies the type of variable.
[] tells the compiler that we are dealing with array.
How to access elements in Array:
arrayName[i]
this will give us the (i+1)th element.
Entering Data into an Array:
Using for loop we can take input from user for elements in array and using scanf() we can assign the value to address of that respective element,&array[i].
Reading Data from Array:
Again using for loop and array[i] we can read data.
Array Initialisation:
and what is happening when just declared not initialised?
int num[3] = { 1,2,3 };
int num[] = { 1,2,3};
- Till the array elements are not given any specific values, they are supposed to contain garbage values.
- If declaration and initialisation done together then no need to specify the size of array.
Array Elements In memory:
int arr[8];
What happens on encountering this?
int arr[8];
16 bytes for this gets reserved in memory.
2 bytes each for 8 nos.
For windows, Linus 4 bytes each hence 32 bytes total.
Since now the storage class is auto, initial values garbage. If it were static it would be zero.
Bounds Checking:
To see we do not reach beyond array size is entirely the programmer’s botheration and not the compiler’s.
The computer may just hang. that program would turn out be suicidal.
Passing Array Elements to a function:
can be passed by a call by value or by reference. really straightforward func(arr[i] ); \: void func( int m ) { ..... } OR func( &arr[i] ); \: void func( int *m ) { ..... }
Pointers increment/decrement:
int i=3,*x; x = &i; x++;
Now x++ will result in the immediately next location of its type(int here).
if int is 2 bytes then x++ will increment by 2 and not 1.
lly for float x++ by 4 and for char by 1.
Pointer and addition, Subtraction :
You can add a number to the pointer. But you cannot add pointer to pointer. j = j + 1; where j,k is pointer. or j = k -5;
Subtraction of one pointer from another:
this is possible for variables of same array.
int arr[ ] = { 10, 20, 30, 40 };
int *j , *i;
j = &arr[1] ; i = &arr[3];
now j - i will be 2 not 4 ( int takes 2 bytes )
as j and i are pointing to locations that are 2 integers apart.
Comparison of 2 pointer variable:
Both should point to same data type.
To test equality or inequality..
Pointer can be compared with zero ( usually expressed as NULL)
Useful when both pointer variables point to elements of the same array.
Never Arithmetic Pointer :
- Addition of 2 pointers.
- Multiplication of a pointer with constant.
- Division of a pointer with constant.