Important concepts and facts in C about Arrays and Pointers

10

Click here to load reader

Transcript of Important concepts and facts in C about Arrays and Pointers

Page 1: Important concepts and facts in C about Arrays and Pointers

Arrays

Array’s name in C is implemented by a constant pointer. It is not

possible to apply increment and decrement on array name ( give

address of the first element in the array.)

Sizeof operator:

-> In case of array gives number of elements .

-> in case of variable , gives its size.

Adding 1 to the address gives the address plus the sizeof

type the array’s elements.

Applying the Address-of operator before the array name gives

the address of the whole array. Adding 1 to this address gives

the address plus the sizeof whole array.

Page 2: Important concepts and facts in C about Arrays and Pointers

ARRAY

In arr[5] : arr=2000 , &arr=2000 arr+1=2004

&arr+1=2020.

Single Pointers can be used to address Multi-

dimensional Arrays.

a[100][100];

Address of a[40][50] = Base address +

40*100*element_size + 50*element_size = 0 +

4000*1 + 50*1 = 4050.

Page 3: Important concepts and facts in C about Arrays and Pointers

In C, we cannot have an array of void type and

function types. For example, below program throws

compiler error

int main() { void arr[100]; }

But we can have array of void pointers and

function pointers. The below program works fine.

int main() { void *arr[100]; }

Page 4: Important concepts and facts in C about Arrays and Pointers

In C/C++, if we initialize an array with fewer members,

all remaining members are automatically initialized as

0.

For example, the following statement initializes an array

of size 1000 with values as 0.

int arr[1000] = {0};

*arr += *(arr + n - 1) += 10; The composite operator

(here +=) has right to left associativity. First 10 is

added to the last element of the array. The result is then

added to the first element of the array.

Page 5: Important concepts and facts in C about Arrays and Pointers

Most languages (Java etc.) prevent the programmer

from going past the end of an array. This process,

performed at runtime by the language

implementation, is frequently called Bounds Checking.

Unfortunately in C there is no way for the programmer

to determine the size of an array at runtime.

//valid int arr[][5] = {}; //valid int arr[][] = {};

//invalid int arr[][10][5] = {}; //valid int arr[][][5] =

{}; //invalid.

Page 6: Important concepts and facts in C about Arrays and Pointers

int main()

{

char p;

char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};

p = (buf + 1)[5];

printf("%d\n", p);

return 0;

}

ANSWER:9 , as p points to 2nd Element + 5 element after

it.

Page 7: Important concepts and facts in C about Arrays and Pointers

http://geeksquiz.com/c-language-2/arrays-pointers/

question:15

int (*p)[5];

p is a pointer to integer array of 5 integers.

In case of “int *p[5]”, p is array of 5 pointers to integers.

int [] fun(void (*fptr)(int *));

A function can’t have an explicit array as return type.

Solution: A pointer can be returned by function by return

statement while providing the size of array via other

mean.

Page 8: Important concepts and facts in C about Arrays and Pointers

sizeof() operator works at compile time.

int * arrPtr[5];

arrPtr is a global array of pointers to int. It should be noted

that global variables are initialized to ZERO. That’s why all

are elements of arrPtr are initialized implicitly to ZERO.

array size can be derived from its initialization but that’s

applicable for first dimension only. It means that 2D array

need to have an explicit size of 2nd dimension. Similarly,

for a 3D array, 2nd and 3rd dimensions need to have

explicit size.

int array2D[2][] = {1,2,3,4,5,6,7,8} //wrong

Page 9: Important concepts and facts in C about Arrays and Pointers

int arr[50] = {0,1,2,[47]=47,48,49};

initialization of array can be done for selected elements as

well , but thereafter sequence starts from that position i.e.

47 and all other elements are initialized to 0.

int arr1[size] = {0}; and int arr2[size];

No issue with definition of arr1 and arr2. In definition of

these arrays, the mention of array size using variable is ok

as per C standard but these types of arrays can’t be

initialized at the time of definition. That’s why initialization

of arr1 is incorrect. But initialization of arr2 is done

correctly.

Page 10: Important concepts and facts in C about Arrays and Pointers

An array whose size is specified as variable can’t be defined out any function. It

can be defined only inside a function. So putting arr[size] outside main() would

result in compile error.

int size = 4;

int arr[size];

int main()

{

if(arr[0])

printf("Initialized to ZERO");

else

printf("Not initialized to ZERO");

return 0;

}