1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> void add(int x, int y, int *s) { *s = x + y; } int main(void) { int x = 4, y = 5, s; add(x, y, &s); printf("sum=%d\n",s); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> #include <stdlib.h> int add(int num[], int n) { int i, sum=0; for (i = 0; i < n; i++) { sum += num[i]; } return sum; } int main(void) { int i,sum,*num; num = (int *)malloc(sizeof(int) * 10); if (num == (int *)NULL) { fprintf(stderr, "malloc fail!!!\n"); exit(1); } for (i = 0; i < 10; i++) { num[i] = rand() % 100 + 1; } sum = add(num, 10); printf("sum=%d\n", sum); } | cs |