Home > Back-end >  Novice c language program problem, the trouble, and have a look
Novice c language program problem, the trouble, and have a look

Time:01-06

Topic is: write a one-dimensional array with n elements sum function sum (), require the use of Pointers in function parameters, and calls to this function in the main function, realization with 10 elements in a one-dimensional array sum,
I wrote: int main ()
{
Int the sum (int * p, int n);
Int a [10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 5};
Int n=10;
The sum (a, n);
Printf (" \ n the array and for % d ", sum);
}
Int the sum (int * p, int n)
{
int i;
int sum=0;
For (I=0; I & lt; n; I++ p++)
The sum +=* p;
Return the sum.
}
There is no error, but the results are not

CodePudding user response:

This call posture,
Printf (" \ n the array and for % d ", sum (a, n));
Then cancel that line on the can,

CodePudding user response:

reference 1st floor forever74 response:
this call posture,
Printf (" \ n the array and for % d ", sum (a, n));
Then cancel that line on the can,

Thank you ╰ '(* ︶ ` *) ╯

CodePudding user response:

 # include & lt; stdio.h> 


Int main ()
{
Int the sum (int * p, int n);
Int a [10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 5};
Int n=10;
//sum (a, n);
Printf (" \ n the array and for % d ", sum (a, n));
}

Int the sum (int * p, int n)
{
int i;
int sum=0;
For (I=0; I & lt; n; I++ p++)
The sum +=* p;
Return the sum.
}


For your reference ~

Or use a variable to receive the sum of the return value, and then print the variable ~ below
  • Related