Upload
burton-coffey
View
11
Download
1
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
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#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);}
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");
}}