1

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", *&deg);
}

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(&degree);

    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;
}
4
  • 1
    inputCoeffs(degree, &coefficients); should be inputCoeffs(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. Commented Mar 1, 2020 at 10:04
  • Hey thanks for that, it worked! Could you explain a little bit regarding that? Because the way I see it is that when I called the inputDegree function in main, 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 for inputCoeffs too? Commented Mar 1, 2020 at 10:15
  • The difference is that degree is not a pointer whereas coefficients is already a pointer. Commented Mar 1, 2020 at 10:20
  • @kaylum my compiler gives a warning a.c:30:25: warning: passing argument 2 of 'inputCoeffs' from incompatible pointer type inputCoeffs(degree, &coefficients); Commented Mar 1, 2020 at 10:20

2 Answers 2

1

In this call

scanf("%d", *&deg);

it is enough to write

scanf("%d", deg);

It is unclear why you are allocating memory one element greater than the value of degree.

coefficients = (double*)malloc((degree + 1) * sizeof(double));

In this case the allocated array has degree + 1 elements.

The type of the second argument in this call

inputCoeffs(degree, &coefficients);

is invalid. There shall be

inputCoeffs(degree, coefficients);
Sign up to request clarification or add additional context in comments.

2 Comments

Regarding the line coefficients = (double*)malloc((degree + 1) * sizeof(double));, I just tried removing the + 1 and my intended program still works. Why is that so? My reasoning for putting that + 1 is that for instance, the inputted degree is 2, the polynomial needs degree + 1 = 3 (A B C, as in Ax^2 + Bx + C) allocated memory.
@PauloVictorio If you allocated less memory than required then the program has undefined behavior. It can work because malloc usually allocates memory of chunks multiplied by the paragraph size.
0

The & operator is used to get a pointer to an existing variable, while * is used to dereference a pointer. Therefore

inputCoeffs(degree, &coefficients);

Makes no sense, because coefficients is already a pointer. inputCoeffs accepts a pointer so you can just write

inputCoeffs(degree, coefficients);

Since the & and * operators basically do the opposite, this line

scanf("%d", *&deg);

is not an error, but could also just be written as

scanf("%d", deg);

Also, you should call free when ever you use malloc, so add to the end of the code:

free(coefficients);
free(integralCoefficients);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.