array-2-ptr.c - A similar C program with pointers - two dimiensional. | Lecture 1 - slide 20 : 34 Program 7 |
#include <stdio.h> #include <stdlib.h> int main(void) { double *b = malloc(2*3 * sizeof(double)), sum1 = 0.0, sum2 = 0.0, *p; int i, j, k; for(i = 0; i < 2*3; i++) /* Initialization */ *(b+i) = (double)(i+1); *(b + 3*1+2) += 0.1; /* b[1][2] += 0.1; */ for (i = 0; i < 2; i++) /* Sum of elements */ for (j = 0; j < 3; j++){ printf("b[%d][%d] = %f\n", i, j, *(b + 3*i+j)); sum1 += *(b + 3*i+j); /* sum1 += b[i][j]; */ } for(i = 0; i < 2*3; i++){ /* Alt summation */ printf("el %d: %f\n", i, *(b+i)); sum2 += *(b + i); } printf("Sum1 = %f, sum2 = %f\n", sum1, sum2); /* Sum1 = 21.100000, sum2 = 21.100000 */ return 0; }