void print( int N, double x[ ] ) { int i; for ( i = 0; i < N; i++ ) printf("%lf\n", x[i] ); } int main( int argc, char *argv[ ] ) { double a[10]; print( a ) ; } |
|
void print( int N, double *x ) { int i; for ( i = 0; i < N; i++ ) printf("%lf\n", x[i] ); // x[i] <==> *(x + i) !!! } int main( int argc, char *argv[ ] ) { double a[10]; print( a ) ; } |
|
|
#define NRows 3 #define NCols 4 int main(int argc, char *argv[]) { int i, j; double* p; // We uses this reference variable to access // dynamically created array elements p = calloc(NRows*NCols, sizeof(double) ); // 3x4 = 12 elements for ( i = 0; i < NRows; i++ ) for ( j = 0; j < NCols; j++ ) p[i*NCols+j] = i+j; // put value i+j in array element p[i][j] for ( i = 0; i < NRows; i++ ) { for ( j = 0; j < NCols; j++ ) printf("p[%d][%d] = %lf ", i, j, p[i*NCols+j] ); putchar('\n'); } free(p); // Un-reserve the first array } |
Result:
p[0][0] = 0.000000 p[0][1] = 1.000000 p[0][2] = 2.000000 p[0][3] = 3.000000 p[1][0] = 1.000000 p[1][1] = 2.000000 p[1][2] = 3.000000 p[1][3] = 4.000000 p[2][0] = 2.000000 p[2][1] = 3.000000 p[2][2] = 4.000000 p[2][3] = 5.000000 |
How to run the program:
|