forked from daction/Basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointer4-Pointer_array_asFunction_arguments.c
More file actions
53 lines (42 loc) · 1.43 KB
/
Pointer4-Pointer_array_asFunction_arguments.c
File metadata and controls
53 lines (42 loc) · 1.43 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Pointer - Array as function arguments (very useful for string)
#include <stdio.h>
#include <stdlib.h>
//Version 1
/*
int SumOfElements(int A[], int size)
{
int i, sum = 0;
for(i = 0; i<size; i++){ // Need to know the size of array A[]
sum += A[i];
}
return sum;
}
int main()
{
int A[] = {1,2,3,4,5};
int size = sizeof(A)/sizeof(A[0]); // Divide whole size of array by size of one integer = number of elements in the array
int total = SumOfElements(A, size);
printf("Sum of elements = %d\n", total);
}
*/
// Mod version 1
//SOE() - sum of elements
int SumOfElements(int* A, int size) // Call by reference
{ // "int* A" or "int A[]" ..it's the same..
int i, sum = 0;
/// int size = sizeof(A)/sizeof(A[0]);
printf("SOE - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));
for(i = 0; i<size; i++){ // Need to know the size of array A[]
sum += A[i]; // A[i] is *(A+i)
}
return sum;
}
int main()
{
int A[] = {1,2,3,4,5};
int size = sizeof(A)/sizeof(A[0]);
/// int size = sizeof(A)/sizeof(A[0]); // Divide whole size of array by size of one integer = number of elements in the array
int total = SumOfElements(A, size); // A can be used for &A[0], but can't do incrementing and decrementing
printf("Sum of elements = %d\n", total);
printf("Main - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));
}