50 likes | 111 Views
2-D Arrays. Pointers & Functions. 2D Arrays and Pointers. char x[4][4] = {{‘a’, ‘b’, ‘c’, ‘d’}, {‘e’, ‘f’, ‘g’, ‘h’}, {‘ i ’, ‘j’, ‘k’, ‘l’}, {‘m’, ‘n’, ‘o’, ‘p’}}; x &x[0][0 ] x[0 ] &x[0][0 ] x[1] &x[1][0] x[2] &x[2][0] *x[2] ? *(x[1]+1) ?.
E N D
2-D Arrays Pointers & Functions
2D Arrays and Pointers • char x[4][4] = {{‘a’, ‘b’, ‘c’, ‘d’}, {‘e’, ‘f’, ‘g’, ‘h’}, {‘i’, ‘j’, ‘k’, ‘l’}, {‘m’, ‘n’, ‘o’, ‘p’}}; • x &x[0][0] • x[0] &x[0][0] • x[1] &x[1][0] • x[2] &x[2][0] • *x[2] ? • *(x[1]+1) ?
2D Arrays and Functions #include <stdio.h> double funct1(int arr1d[], int n1d, double arr2d[][5],int n2d_r, int n2d_c); int main(void) { • int a[20]; double b[100][5]; double res; ….. • res = funct1(a, 20, b, 100, 5); …. return(0); } • double funct1(int arr1d[], int n1d, double arr2d[][5],int n2d_r, int n2d_c) { … return(result); }
Example printf("%x\n", &Arr[0][0]); printf("%x\n", Arr[0]); printf("%x\n", Arr[1]); printf("%x\n", ptr); printf("%d\n", Arr[0][0]); printf("%d\n", *ptr); printf("%d\n", *(++ptr)); printf("%x\n", *(++ptr)); printf("%x\n", *(ptr+3)); printf("%d\n", *(Arr[1] + 1)); printf("%d\n", *(Arr[1] + 2)); printf("%x\n", *(Arr + 2)); printf("%x\n", *(*(Arr + 2))); printf("%x\n", *(*(Arr + 2) + 1)); return(0); } #include <stdio.h> #include <stdlib.h> #include <time.h> void Fill2Darr(intarr[][3], intrw, intcl); void Prnt2Darr(intarr[][3], intrw, intcl); int main(void) { intArr[4][3]; int *ptr; ptr = Arr[0]; Fill2Darr(Arr, 4, 3); Prnt2Darr(Arr, 4, 3);
Example void Prnt2Darr(intarr[][3], intrw, intcl) { inti, j; for(i=0; i<rw; i++) { for(j=0; j<cl; j++) printf("%d ", arr[i][j]); printf("\n"); } } void Fill2Darr(intarr[][3], intrw, intcl) { inti, j; srand((unsigned) time(NULL)); for(i=0; i<rw; i++) for(j=0; j<cl; j++) arr[i][j] = rand()%10; }