I play with some C programs to learn C and I have a question.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void array_sum(int a[], int n);
int main()
{
int i,size;
int *p=(int *)malloc(sizeof(int) * size);
int buffer;
printf("\nHow many numbers you want to enter?\n");
scanf("%d", &size);
printf("\nEnter %d numbers:\n", size);
for(i = 0; i < size; i++)
{
scanf("%d", &buffer);
p[i] = buffer;
}
array_sum(p, size);
return 0;
}
void array_sum(int a[], int n)
{
int i,sum;
sum=0;
for(i = 0; i < n ; i++)
{
sum = sum + a[i];
}
printf("\nHere is the sum: %d\n", sum);
}
This program works fine, my question is how I can call the sum value from the out of the function. Such as I want to take out this code from the function and used under the main()
Code:
printf("\nHere is the sum: %d\n", sum);