Results 1 to 7 of 7

Thread: C question: pointers

  1. #1
    Junior Member
    Join Date
    Feb 2005
    Posts
    2

    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. #2
    SG DC Team Member Paft's Avatar
    Join Date
    Feb 2001
    Location
    Charlottesville, VA
    Posts
    5,736
    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. #3
    Junior Member
    Join Date
    Feb 2005
    Posts
    2

    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. #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. #5
    SG DC Team Member Paft's Avatar
    Join Date
    Feb 2001
    Location
    Charlottesville, VA
    Posts
    5,736
    Quote 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?

    Quote 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.

    Quote 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.
    Last edited by Paft; 03-09-05 at 05:12 PM.

  6. #6
    Member Firestorm ZERO's Avatar
    Join Date
    Nov 2000
    Location
    Toronto, Ontario CANADA
    Posts
    59
    [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)
    Last edited by Firestorm ZERO; 03-09-05 at 08:26 PM.

  7. #7
    SG DC Team Member Paft's Avatar
    Join Date
    Feb 2001
    Location
    Charlottesville, VA
    Posts
    5,736
    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.

Similar Threads

  1. Ask a question... any question!
    By MadDoctor in forum General Discussion Board
    Replies: 69
    Last Post: 05-09-02, 12:54 AM
  2. LOBO THE GOD OF TWEAKING dual boot and nic question
    By mccoffee in forum Broadband Tweaks Help
    Replies: 10
    Last Post: 04-16-02, 10:18 PM
  3. Replies: 7
    Last Post: 03-03-02, 02:46 AM
  4. VERY TOUGH QUESTION (joke)
    By lurker in forum General Discussion Board
    Replies: 3
    Last Post: 10-23-01, 01:50 PM
  5. A moral question
    By Leatherneck in forum General Discussion Board
    Replies: 24
    Last Post: 09-29-01, 02:35 PM

Bookmarks

Posting Permissions

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