1 of 22

CSO101: Computer Programming

Pointers - Arrays - Functions

O.S.L. Bhavana

2 of 22

[ ] Array subscript operator

  • [] should be used with pointers/arrays
  • This operator the effect of pointer arithmetic and dereferencing (in places other than array declaration)
  • a[b] has the effect of *(a+b)
  • Ex: int a[5]={0}; a[2] // the expression a[2] has the effect of *(a+2); infact a[2] or 2[a] have the same effect
  • [ ] operator can also be used with pointers
  • Ex: int a[5]={0}, *ptr=a; ptr[2] also has the effect of *(ptr+2)
  • int a[3][4]; a[i][j] = *(a[i]+j) = *(*(a+i)+j)
  • Precedence of [] is higher than * and &, therefore parentheses is must
  • Associativity of [] is left-to-right, associativity of * and & is right-to-left
  • x[y] = *(x+y) by the definition of the index/subscript operator

3 of 22

Pointer to an array

  • int (*ptr)[5]; // In this declaration
    • (*ptr) emphasises ptr is a pointer
    • (*ptr)[5] → ptr points to an 1D-array of 5 elements
    • int (*ptr)[5] → ptr points to an 1D-array of 5 integer elements
    • i^th element of this 1D array is given by (*ptr)[i]
    • Value of ptr+1 is value of ptr + 20bytes
  • int (*ptr)[5][2];
    • (*ptr) emphasises ptr is a pointer
    • (*ptr)[5][2] → ptr points to a 2D-array of 5*2 elements
    • int (*ptr)[5][2] → ptr points to an 2D-array of 5*2 integer elements
    • (*ptr)[i] - accesses the i^th row
    • (*ptr)[i][j] - accesses the j^th element in i^th row
    • Value of ptr+1 is value of ptr + 40bytes

4 of 22

Pointer to an array

variable

Value

Points to

Data Type (from the perspective ofpointers)

int a = 10; //&a=1000;

a

10

-

int

&a

1000

-

int *

Array name with reference operator

Starting address of the array

The entire array

Array name

Starting address of the array

A[0] //when the array name is A

int a[5];//let 2000 be starting address of the array

a[i]

int value

-

int

a

&a[0]

a[0]

int *

&a

&a[0] = a

Entire array of 5 elements

int (*)[5];//pointer to an array

5 of 22

Pointer to an array

variable

Literal type

Value

Points to

Data Type (from the perspective ofpointers)

Dereferene

Array name with reference operator

Starting address of the array

The entire array

Array name

Starting address of the array

A[0] //when the array name is A

int a[5][2]; //2000 be the add

a[i][j]

int

int value

-

int

-

a[i]

1D Array

&a[i][0]

a[i][0]

int *

*a[i] = *(a[i]) = �*(*(a+i)) = a[i][0]

a

2D Array

&a[0]

a[0]

int (*)[2]; //pointer to an array of 2 int

*a = *(&a[0]) = a[0]

&a

&a

Entire 2D-array of 5*2 elements

int (*)[5][2]

*(&a) = a

6 of 22

Pointer to an array

variable

Literal type

Value

Points to

Data Type (from the perspective ofpointers)

Dereferene

Array name with reference operator

Starting address of the array

The entire array

Array name

Starting address of the array

A[0] //when the array name is A

int a[5][2][2]; //2000 be the add

a[i][j][k]

int

int value

-

int

a[i][j]

1D Array

&a[i][j][0]

a[i][j][0]

int *

*a[i][j] = a[i][j][0]

a[i]

2D Array

&a[i][0]

a[i][0]

int (*)[2]; //pointer to an array of 2 int

*a[i] = a[i][0]

a

3D Array

&a[0]

a[0]

int (*)[2][2]

*a = a[0]

&a

&a

Entire 3D-array of 5*2*2 elements

int (*)[5][2][2]

a

7 of 22

8 of 22

Recall

  • int arrA[m]; // arrA[i] = *(arrA+i)
  • int arrB[m][n];
    • // arrB[i][j] = *(*(arrB+i)+j);
    • = *((int *)arrB + i*n +j)
  • int arrC[m][n][p];
    • arrC[i][j][k] = *((int *)arrC + i*n*p + j*p + k)
    • = *(*(*(arrC+i) + j) + k)
    • arrC - pointer to arrC[0]
    • arrC+i - pointer to arrC[i]
    • *(arrC+i)

9 of 22

Pointer Arrays:

  • What is the output of the following code?
  • int *pa[4]; //declares pa is an array of 4 pointers

10 of 22

n-D arrays & Pointers

  • int a[3][4][2] = {{{3,4,9},{12,17}}, {{21,29,20,24},{36,33,38}}, {{49,42,43,41},{56,53,52,55}}}; //
  • //let the starting address of the first element in the multi-dim array be 1000.
  • What is the value of a[1]?
  • What is the value of a[1]+1?
  • What is the value of a[3][0]?

11 of 22

n-D arrays & Pointers

  • int a[3][4][2] = {{{3,4,9},{12,17}}, {{21,29,20,24},{36,33,38}}, {{49,42,43,41},{56,53,52,55}}}; //
  • //let the starting address of the first element in the multi-dim array be 1000.
  • What is the value of a[1]? (a[1] treated as address/pointer) 1032
  • What is the value of a[1]+1? (a[1] treated as pointer) 1040
  • What is the value of a[3][0]? (treated as address/pointer) 1096

12 of 22

n-D Arrays & Functions

  • Multi-dimensional Arrays can be passed a arguments to functions as
    1. A multi-dimensional array:

    • As a “pointer to an array”

    • As a pointer (with a type cast)

13 of 22

Multi-dimensional arrays as function arguments

  • All except the first dimension are compulsory to mention like in the declaration
  • If the array dimensions are constants, then they can be mentioned in the function declaration as follows

Ex: Function declaration: int max(int a[2][3]);

Function call: max(a);

Ex: incorrect function declaration: int max(int a[][]); //second dim missing

  • If the array dimensions are variables, then these variables can be declared in the function declaration too
  • Ex: Function declaration: int max(int x, int y, int a[x][y]); // here the dimensions of the 2D matrix are allowed to be variables. Note that the dimension variables x,y are declared before a.
  • Function Call: int x,y; scanf(“%d %d”,&x,&y); int a[x][y] max(x,y,a);

14 of 22

Multi-dimensional arrays as function arguments

  • Finding the max of elements of a 2Dmatrix

15 of 22

Passing as a pointer to a single element

  • Multi-dimensional Arrays can be passed as arguments to functions as a data_type pointer (with a type cast)

16 of 22

Determinant Program

int det(int *a, int n);

int main()

{ int arr[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

printf("%d", det((int *)arr,4)); return 0;}

int det(int *a, int n)

{ if (n==1) {return *a;}

else {int sum =0;

for(int i=0;i<n;i++)

{ int b[n-1][n-1];

for(int j=0;j<n-1;j++)

{ for(int k=0;k<n-1;k++)

{int k1 = (k<i?k:k+1), j1 =j+1;

b[j][k] = *(a+j1*n+k1); } }

sum += *(a+i)* (i%2?-1:1) * det((int*)b,n-1); }

return sum; }

}

17 of 22

Array Bounds Checking

  • In C there is no check to see if the subscript used for an array index is within the the array size;
  • Therefore, programs have to be written carefully to ensure that the index is within the size.
  • What does the following snippet print?

    • int a[10]={0}, *p=a;
    • printf("%d %d", a[-1], *(p+10));

18 of 22

Passing as a Pointer to an Array

19 of 22

Pointer Arrays or Array of Pointers

  • Pointers are variables themselves, they can be stored in arrays just as other variables
  • Declaration Syntax: int *pa[4]; // declares an array of four pointers
  • Initialization and declarations rules are same as that of arrays.
  • What is the output of the following code? Ans:1 2 3 4

20 of 22

Returning multiple values from function

  • This is an indirect method of returning values
  • The calling function can create variables to store the values that it desires to be returned
  • It can then pass these variables by reference to the called function as an argument
  • The called function can then assign return values as dereference of these pointers

21 of 22

Returning multiple values from function

22 of 22

Returning Arrays From Function

  • Arrays can be returned as pointers from functions
  • If the array is a local variable of the function warning is issued
  • The array may be declared as static array to avoid this.