1. ## C question: pointers

hi,

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 clocalfolder)/polyTest.txt
#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("colyTest.txt","w");

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[i] = 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

2. Uh, you do realize that coef[n+1] goes out of scope at the end of polyInit(), right? You're pointing to a garbage address with *poly.

3. ## oh-- now i know

thanks, I didn't know that!!

I was going to ask another question, but I guess I ought to go read up on runtime memory allocation first. maybe I can figure this out myself.

again, thanks

4. ## Chandra

There are few bugs in ur problem.I'm listing them one by one.See if u gain anything from it.
1) I don't know how ur program compiler but the line
float coef[n+1]; gives compiler error since a constant value is required as array size.This is compile time information & the compiler need to have a value ather then a variable.
2) Never allocate a local array & pass its base address outside the scope.When function ends the local array "coef " gets out of scope & the memory is released,so u know what u will get outside this function
3) If at all u want memory for poly to be allocated inside polyInit,either pass the address of this pointer or pass the pointer as reference.Dyanamically allocate memory for poly inside the function & make arrangements for deallocating the memory when ue purpose is fulfilled.

5. Originally Posted by Chandra Prakash
1) I don't know how ur program compiler but the line
float coef[n+1]; gives compiler error since a constant value is required as array size.This is compile time information & the compiler need to have a value ather then a variable.
That's utter crap, and wrong. The compiler handles this. Here's code that works fine:

Code:
```#include <iostream>

int main()
{
char array[3] = "Hi.";

for (int x = 0; x < 3; x++)
{
std::cout << array[x];
}

return(0);
}```
ALSO, if it was compile-time information, how the f(x) would the 'new' operator work?

Originally Posted by Chandra Prakash
2) Never allocate a local array & pass its base address outside the scope.When function ends the local array "coef " gets out of scope & the memory is released,so u know what u will get outside this function
The only way that this is acceptable is if you're calling a function from within a function, and passing the array that way. Otherwise Chandra is correct.

Originally Posted by Chandra Prakash
3) If at all u want memory for poly to be allocated inside polyInit,either pass the address of this pointer or pass the pointer as reference.Dyanamically allocate memory for poly inside the function & make arrangements for deallocating the memory when ue purpose is fulfilled.
malloc() and free() are good ways of doing this.

6. [QUOTE=Paft]That's utter crap, and wrong. The compiler handles this. Here's code that works fine:

Code:
```#include <iostream>

int main()
{
char array[3] = "Hi.";

for (int x = 0; x < 3; x++)
{
std::cout << array[x];
}

return(0);
}```
[/QUOTE=Paft]

I think what Chandra meant is. Yes you can use array with a variable for the index in loops and such. But you can't DECLARE an array with a variable.

This is because in C, the memory allocated for an array is put in sequencal (sp?) order. So the compiler must know how much memory to allocate for the array before it can declare it. So...
float coef[5] is ok but
float coef[x] is not

To go around that you have to dynamically allocate the memory. Which Chandra pointed out in (3)

7. Ohhh, I get what you're saying. And that's correct. To dynamically allocate the memory, you can look into malloc() and free() (I believe it's free()); or new and delete.

Sorry.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•