i'm pretty new to C programming, but i'm trying to learn fast for work. any help would be greatly, greatly appreciated
i'm trying to implement a function with a lot of polynomial arithmetic. i'm representing each polynomial as an array of floats-- the floats are the polynomial's coefficients for each term and the array index is the degree of that term. ex: 1.2 x^3 + 3.4 x + 5 is: float poly[4] = {5, 3.4, 0, 1.2}.
In the main program, I'd like to just be able to use float pointers to point to the coefficient arrays. In fact, it'd be simplest for me to allocate memory for each polynomial (coefficient array) inside a polynomial initialization function and not have to worry about that in the main program, either. (I'll leave out my reasons for this for the sake of brevity). Here's an example test program of what I'm doing:
// test code for polyInit
// polyTest.c: output printed to c
#include <stdio.h>
// local function declaration
void polyInit(float *poly_ptr, int n);
main() {
FILE *output;
int i = 0;
int poly_order = 8;
float *poly; // pointer to polynomial coefficients
output = fopen("c
polyInit(poly, poly_order);
fprintf(output,"initial value pointed to by poly pointer: %f\n", *poly);
fprintf(output,"initial values for polynomial coefficients are: \n");
for(i=poly_order; i>=0; i--) {
fprintf(output, "%f x^%i ", *(poly+i), i);
if(i!=0) fprintf(output,"+ ");
}
}
// allocate memory for an array of floats of length n+1 and point poly_ptr at
// this array
void polyInit(float *poly_ptr, int n) {
int i = 0;
float coef[n+1];
for(i=0; i<=n; i++)
coef = 0;
poly_ptr = coef;
return;
}
Unfortunately, there must be some flaw to my logic. This program will compile, but won't run unless I comment out both fprintf's. So obviously the float pointer is not being "initialized", because any reference to what the pointer is pointing at is causing an error. Any thoughts on what I'm doing wrong and/or what I should do differently?
thanks!
Luan