指標_陣列加總

 avatar
user_3763047219
c_cpp
2 years ago
452 B
1
Indexable
Never
#include <iostream>
#include <stdio.h>

int mysum(int* a, int size);

int mysum(int* a, int size) {
    int sum = 0;
    for (int i = 1; i <= size; i++) {
        sum = sum + *a;
        a++;
    }
    return sum;
}
int main() {

    int data[4] = {};
    for (int i = 0; i < 4; i++) {
        scanf("%d", &data[i]);
    }
    int size = sizeof(data) / sizeof(data[0]);
    printf("%d\n", mysum(data, size));

    return 0;

}