CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2005
    Posts
    3

    newbie C question

    I'm still a bit new to C, and attempting to master pointers. I have a function that allocates a 2d pointer array, like so:

    Code:
    int main(int argc, char *argv[]){
      float **data;
    
      alloc_data(&data);
    }
    
    void alloc_data(float ***data){
      int i, nx, ny;
      float **tmp;
    
      nx = 3;
      ny = 3;
    
      tmp = (float **) calloc(nx, sizeof(float *));
      for (i=0; i<nx; i++)
        tmp[i] = (float *) calloc(ny, sizeof(float));
    
      *data = tmp;
    }
    works just fine, but how can I allocate data without having to alloc tmp and have data point to tmp? For example, if I do:

    Code:
      *data = (float **) calloc(nx, sizeof(float *));
      for (i=0; i<nx; i++)
        *data[i] = (float *) calloc(ny, sizeof(float));
    it doesn't work as I expect - what am I missing here?

    thanks for any help.

    -Tim

  2. #2
    Join Date
    Dec 2005
    Posts
    3

    Re: newbie C question

    [] has higher precedence than *, so the compiler interperets *data[i] as *(data[i]) when what you want is (*data)[i].

  3. #3
    Join Date
    Dec 2005
    Posts
    3

    Re: newbie C question

    Aha! Thanks! Now I'll go learn my C precedence rules.

    -Tim

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: newbie C question

    Another way is to pass the double pointer as a reference:
    Code:
    void alloc_data(float** &data) // pass reference (to the double pointer)
    {
        int i, nx, ny;
    
        nx = 3;
        ny = 3;
    
        data = (float **) calloc(nx, sizeof(float *));
        for (i=0; i<nx; i++)
            data[i] = (float *) calloc(ny, sizeof(float));
    }
    
    int main(int argc, char *argv[])
    {
        float **data;
        alloc_data(data);
    }
    - petter

  5. #5
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: newbie C question

    I agree with Wildfrog; that is the method of choice in my opinion
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  6. #6
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: newbie C question

    Quote Originally Posted by Eli Gassert
    I agree with Wildfrog; that is the method of choice in my opinion
    The only problem: C++ might understand references, but C does not. The OP stated to use C so your solution won't help him at all.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: newbie C question

    Quote Originally Posted by NoHero
    The only problem: C++ might understand references, but C does not. The OP stated to use C so your solution won't help him at all.
    Oh, I thought he was just another lazy C++ programmer (and dropped the obvious ++)

    Well, then I'd go for evilsaltines solution:
    Code:
    void alloc_data(float*** data) // pass pointer (to the double pointer)
    {
        int i, nx, ny;
    
        nx = 3;
        ny = 3;
    
        *data = (float **) calloc(nx, sizeof(float *));
        for (i=0; i<nx; i++)
            (*data)[i] = (float *) calloc(ny, sizeof(float));
    }
    
    int main(int argc, char *argv[])
    {
        float **data = 0;
        alloc_data(&data);
    }
    - petter

  8. #8
    Join Date
    Dec 2005
    Posts
    3

    Re: newbie C question

    The code is currently in C, but I am fiddling a bit with C++ as well, so thanks for pointing out the handy pointer reference trick for C++.

    -Tim

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured