Chp 8 Arrays Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are arrays?

A

An array is a collective name given to a group of ‘similar quantities.’
Aka Subscripted variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Rules of Array:
how are elementws stored?
Indexing?
Declaring?
etc
A
  1. All member elements must be of same type.
  2. All array elements are numbered starting from 0.
  3. Hence last element is size-1
  4. Bfore using array its type and dimension must be declared.
  5. Elements of array are always stored in contiguous (adjacent) memory locations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Array Declaration:

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to access elements in Array:

A

arrayName[i]

this will give us the (i+1)th element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Entering Data into an Array:

A

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].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Reading Data from Array:

A

Again using for loop and array[i] we can read data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array Initialisation:

and what is happening when just declared not initialised?

A

int num[3] = { 1,2,3 };
int num[] = { 1,2,3};

  1. Till the array elements are not given any specific values, they are supposed to contain garbage values.
  2. If declaration and initialisation done together then no need to specify the size of array.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Array Elements In memory:

int arr[8];

What happens on encountering this?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Bounds Checking:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Passing Array Elements to a function:

A
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 ) { ..... }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Pointers increment/decrement:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Pointer and addition, Subtraction :

A
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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Subtraction of one pointer from another:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Comparison of 2 pointer variable:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Never Arithmetic Pointer :

A
  1. Addition of 2 pointers.
  2. Multiplication of a pointer with constant.
  3. Division of a pointer with constant.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

in a for loop if u print out &arr[i], not beyond bounds, What will u get?

A

You will prove that elements are always stored in contiguous memory locations and that pointer when incremented points to immediately next location of its type.
if arr has ints then address printed will differ by 2 bytes (if int takes space of 2 bytes).

17
Q

Base Address:

A

address of 0th element

passing &arr[0] is equivalent to passing arr, just name.

18
Q

Access elements of arr. Efficient method:

A

accessing elements by pointers is always the fastest.
[ where u set j as base address and then in for loop j++ in body, in for statement i from 0 to size of arr minus 1]

Especially for fixed order.
If not fixed order then easier to access by subscript ( the i , arr[i] ) but still pointer method will be faster.

19
Q

Passing and entire Array to a Function:

A

You could just pass the base address and the size of array. Incrementing address and using * (value at address) will give you the entire array.

20
Q

what does C compiler do internally when we say num[i]:

and derive further fascinating relations

A

it does *(num + i ) when we say num[i]
now
num[i] = *(num + i ) = *( i + num) = i[num]

21
Q

Two Dimensional Array introduction, what its like:

A

collection of one dimensional array placed on below another.
you can thus picture rows and columns. Matrix
Traditionally: array elements are being stored and accessed rowwise.
int arr[r][c];

22
Q

Initialising 2 D Array:

A
int arr[4][2] = {
                         { 101, 90},
                         { 102, 98},
                         { 103, 50},
                         { 104, 96},
                       };
OR
int arr[4][2] =  { 101, 90, 102, 98,103, 50, 104, 96};

Necessary to mention column, rows can be skipped.

23
Q

Memory Map of 2 D array:

A

Elements stored in one continuous chain.

s[0][0], s[0][1], s[1][0],s[1][1],s[2][0],s[2][1]…

24
Q

Pointers and 2D Arrays:

and various ways to represent an element:

and C’s approach to decode int s[5][2];

A

C can treat parts of arrays as arrays.
int s[5][2];
can be thought of as, an array of 5 elements, where each element is a one-dimensional array of 2 elements.
Now s[i] will give the address of the ith array.
In this case s[1] will be great than s[0] by 4 bytes (if int takes 2 bytes space).
now s[i][0] has address of s[i] and next is s[i][1] which has address s[i]+1.
so, s[i][j] = *( s[i] + j ) = *( *( s + i ) +j )
last one from 1D array.

25
Q

Pointer to an array:

A

int (*p)[2];
parenthesis necessary.
p is a pointer to array of 2 integers.
So we can pass address one dimensional arrays with 2 integers to store in p.

now those 1D arrays can be member of 2 D arrays.

26
Q

Passing 2D array to a function:

A

We can pass address of (0,0) element, simply the name of array (say s).
then use of:
s + (i* column ) + j to give address of element req.
now i will loop until i < row
and j until j < col
(s + (icolumn) + j)

or we could pass s, NO of rows and columns
and collect it as int *(q)[][4], r, c

then nested for loop. Outer for will increase q
inner will travel through elements of 1D array then.

or simply we can use q[i][j] in nested for loop.
collecting q as int q[ ][4],

27
Q

Array of pointers:

A

Collection of addresses.
It can be of any addresses.

int *p[4];
p is an array which contains pointers.

*p[i] will give us value at ith address.

Ans pg 308 printf();

28
Q

3 D Array:

and memory map

A

It can be thought of as
array of 2D arrays
2D arrays is array of 1D arrays
int arr[3][4][2];
Elements of 3D array will be 3 two D array of 4 one D array of 2 elements.
You can think of a cube.
But in memory they are stored linerly.
0th 2D, 1st 2D , 2nd 2D.
2,4,7,8,3,4,5,6, 7,6,3,4,5,3,2,3, 8,9 ,7,2 ,3,4 ,5,1
to access an element say ‘1’ we can use:
first subscript 2, as its in 2nd twoD array, then in 3rd oneD array and its first element. Since numbering starts from 0.

arr[2][3][1]
*( *( *( arr + 2 )+ 3 )+ 1 )