logo

How to pass arrays to functions in C

#include <stdio.h> #include <stdlib.h> #include <string.h> int sum(int* arr, int n) { int i, s = 0; arr[0] = 10; for (i = 0; i < n; i++) { s += arr[i]; } printf("Size from sum function: %llu\n", sizeof(arr)); return s; } int main(int argc, char* argv[]) { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int s = sum(arr, 7); printf("Sum: %d\n", s); printf("First element: %d\n", arr[0]); printf("Size: %llu\n", sizeof(arr)); return 0; }

Courses with this lesson