1 / 5

2-D Arrays

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)  ?.

Download Presentation

2-D Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2-D Arrays Pointers & Functions

  2. 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)  ?

  3. 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); }

  4. 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);

  5. 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; }

More Related