5
2-D Arrays Pointers & Functions

2-D Arrays

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 2-D Arrays

2-D Arrays

Pointers & Functions

Page 2: 2-D Arrays

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

Page 3: 2-D Arrays

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

Page 4: 2-D Arrays

Example#include <stdio.h>#include <stdlib.h>#include <time.h> void Fill2Darr(int arr[][3], int rw, int cl);void Prnt2Darr(int arr[][3], int rw, int cl); int main(void) {

int Arr[4][3];int *ptr;ptr = Arr[0];

Fill2Darr(Arr, 4, 3);Prnt2Darr(Arr, 4, 3);

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

Page 5: 2-D Arrays

Examplevoid Fill2Darr(int arr[][3], int rw, int cl) {

int i, j;

srand((unsigned) time(NULL));

for(i=0; i<rw; i++)for(j=0; j<cl; j++)

arr[i][j] = rand()%10;}

void Prnt2Darr(int arr[][3], int rw, int cl) {int i, j;

for(i=0; i<rw; i++) {

for(j=0; j<cl; j++)printf("%d ",

arr[i][j]);printf("\n");

}}