'Receiving unexpected return value. Having problems with pointers in C
I am learning the use of pointers, but for some reason my code makes the terminal give an unexpected return value. There is no warning shown in the log, thus I am unsure what's causing the problem. Any help is highly appreciated! My code:
#include <stdio.h>
int findArraySum(int *pArr, int ArrSize)
{
int i,sum=0;
for(i=0;i<ArrSize;i++)
{
sum+=pArr[i];
}
return sum;
}
int main()
{
int arraySize,i;
printf("Size of array: ");
scanf("%d",arraySize);
int array[arraySize];
for(i=0;i<arraySize;i++)
{
printf("Element [%d]: ",i+1);
scanf("%d",array[i]);
}
printf("Sum of array: %d",findArraySum(array,arraySize));
return 0;
}
I have tried using
//more code
int findArraySum(int *pArr, int *pArrSize)
{
int i,sum=0;
for(i=0;i<pArrSize;i++)
//more code
but to no avail, and I also received warning messages for making a pointer from an integer without a cast and for comparing pointer to integer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
