The following code block is only part of a larger one. In the program, I want to find the definite integral of a polynomial that needs to be inputted by the user.
I'm new to C and so I'm having a hard time trying to learn the syntax with regards to pointers. I find them very confusing.
So if you'll look at the code block below, I want to print the elements contained in the array coefficients just so I can see if the elements inputted are being stored in the array but to no avail. The program just terminates after the inputCoeffs() function.
#include <stdio.h>
#include <stdlib.h>
void inputDegree(int *deg) {
printf("Enter the degree of the polynomial: \n");
scanf("%d", *°);
}
void inputCoeffs(int deg, double *coeffs) {
printf("Enter the coefficients of the polynomial (A, B, C,...): \n");
for(int i = 0; i <= deg; i++) {
scanf("%lf", &coeffs[i]);
}
}
int main() {
int i;
int degree;
double lowerLimit;
double upperLimit;
double integral;
double *coefficients = NULL;
double *integralCoefficients = NULL;
inputDegree(°ree);
coefficients = (double*)malloc((degree + 1) * sizeof(double));
integralCoefficients = (double*)malloc((degree + 1) * sizeof(double));
inputCoeffs(degree, &coefficients);
for(i = 0; i <= degree; i++) {
printf("\t%lf\n", coefficients[i]);
}
return 0;
}
inputCoeffs(degree, &coefficients);should beinputCoeffs(degree, coefficients);. I think the compiler should have had a warning on that line as the passed in type for the second arg is different to the type defined by the function. If not, turn up your compiler warnings. Always heed and fix all compiler warnings.inputDegreefunction inmain, it had a&in its argument, considering that the function needs a pointer to be passed to it. So I thought maybe that's how it works forinputCoeffstoo?degreeis not a pointer whereascoefficientsis already a pointer.a.c:30:25: warning: passing argument 2 of 'inputCoeffs' from incompatible pointer type inputCoeffs(degree, &coefficients);