CSO101: Computer Programming
Pointers - Arrays - Functions
O.S.L. Bhavana
[ ] Array subscript operator
Pointer to an array
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 |
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 |
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 |
Recall
Pointer Arrays:
n-D arrays & Pointers
n-D arrays & Pointers
n-D Arrays & Functions
Multi-dimensional arrays as function arguments
Ex: Function declaration: int max(int a[2][3]);
Function call: max(a);
Ex: incorrect function declaration: int max(int a[][]); //second dim missing
Multi-dimensional arrays as function arguments
Passing as a pointer to a single element
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; }
}
Array Bounds Checking
Passing as a Pointer to an Array
Pointer Arrays or Array of Pointers
Returning multiple values from function
Returning multiple values from function
Returning Arrays From Function