Double Pointer Question

This is a discussion on Double Pointer Question within the C Programming forums, part of the General Programming Boards category; Hi, new to these boards and they seem like a great place to spend some time. I have a question ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    11

    Post Double Pointer Question

    Hi, new to these boards and they seem like a great place to spend some time. I have a question about double pointers to structs that I'm hoping someone can help me out with. If I have a double pointer to an array of structs, how can I get at a member of the nth struct?

    Let me explain that in code. The func is a struct:

    Code:
    func ** functions = xmalloc( foo * sizeof( func * ) );
    unsigned int numDone = 0;
    
    while( i = 0; i < bar; ++i )
    {
        *( func + numDone++ ) = makeFunc(); //returns a func *
    }
    
    //print the member "numInsts" from the nth function:
    printf( "%u\n", *(*(func + n )).numInsts );
    But, that doesn't compile. So, how do I dereference the double pointer and get myself a struct to work with. I feel like I'm just missing a subtlety in the syntax.

    Thanks for any help,
    Matt

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by krock923 View Post
    Code:
    func ** functions = xmalloc( foo * sizeof( func * ) );
    unsigned int numDone = 0;
    
    while( i = 0; i < bar; ++i )
    {
        *( func + numDone++ ) = makeFunc(); //returns a func *
    }
    But, that doesn't compile. So, how do I dereference the double pointer and get myself a struct to work with. I feel like I'm just missing a subtlety in the syntax.

    Thanks for any help,
    Matt
    I don't know what error you're getting, since I don't feel like making a testable .c file out of this, but I'm going to guess: *(func + numDone++) complains that func is a type (not the variable functions) and can't be added to. Do I win?

    Not compile errors, but: shouldn't bar = foo? (Unless you're allocating more memory than you need.) Why not use bar inside the loop as
    Code:
    *(functions + bar) = makeFunc(); //?
    [Of course, if this for loop occurs in multiple places so that you're not doing them all at once, then, well, ok, I guess.]

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    Yes, bar should = foo. That was just a typo on my part and they are indeed the same values in my code.

    Unfortunately, you don't win at the error guessing game Here's the error I get:

    error: request for member 'numInsts' in something not a structure or union

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Try (**(func+n)).numInsts instead. Dot associates before the outer * otherwise, IIRC. (Actually I didn't recall that at all, I had to try things )

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    Yup, that did it. Thank you for the help.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    xmalloc()?? Is that one of your functions? I've never heard of it.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    It's something I read in a GNU doc somewhere - I just had to implement it. It calls malloc, checks for the null pointer and kills the program if it finds one.

    I figure if the runtime system can't get me anymore memory, a kernel panic isn't too far anyway and there's no use trying to forge ahead.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by krock923 View Post
    Code:
    func ** functions = xmalloc( foo * sizeof( func * ) );
    unsigned int numDone = 0;
    
    while( i = 0; i < bar; ++i )
    {
        *( func + numDone++ ) = makeFunc(); //returns a func *
    }
    
    //print the member "numInsts" from the nth function:
    printf( "%u\n", *(*(func + n )).numInsts );
    Why would you allocate a number the items 'foo' contains, and then loop up to 'bar', and then print item 'n'? I realise you've tried to hide what the code really looks like by using foo and bar etc, but in this case it is only making more questions arrise as it does not make sense. Post the real code!

    You're using the syntax for a for-loop, but it says 'while' instead.
    You don't need 'numDone' as it is the same value as i.
    You're not assigning to the array 'functions', you're assigning to a type instead which does not make sense. That's not how you're supposed do array syntax anyway. It's:
    Code:
    a[i] = blah;
    Then inside your printf it's simply
    Code:
    a[i]->numInsts
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by tabstop View Post
    Try (**(func+n)).numInsts instead. Dot associates before the outer * otherwise, IIRC. (Actually I didn't recall that at all, I had to try things )
    Apart form the fact that you're still assigning to a type instead of an array, why would you suggest such horrible syntax when he can simply use functions[n]->numInsts?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    Sorry, I was trying to not show parts that are unnecessary. To point out your arguments, which are absolutely rational from only seeing that fragment, I'll admit:

    The foo/bar thing was pointed out a few posts up.
    numDone has other stuff done to it between its initialization and that loop. Those things just aren't necessary in the context of the question I asked.
    The func thing was a typo. Where it says func in that assignment, it's a variable name in my code.

    Thanks for the pointer (groan!) on how to do arrays. I'm not sure why I insist on doing that whenever I write C. I suppose that it makes things harder than necessary. The reason that I didn't use -> was because I honestly had no idea you could do that in C. I thought that was only a C++ thing. It really makes much more sense that way, though.

    At first glance, your avatar looks like some variation of selection sort, but that's probably too simple if you really are someone that is into algorithm analysis.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by iMalc View Post
    Apart form the fact that you're still assigning to a type instead of an array, why would you suggest such horrible syntax when he can simply use functions[n]->numInsts?
    I just give the man what he wants. Based on the inscrutability of the code posted, I didn't dare ask, really.
    Last edited by tabstop; 12-07-2007 at 10:18 PM. Reason: hadn't stopped typing yet

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    Ouch

    It's finals time and I haven't slept much this past week. . .

    Rest assured that it all is correct inside the actual source file. gcc is good about complaining when the code is that stupid.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Yeah sorry, most of the posts above mine weren't showing when I posted.
    I know what it's like to forget what is part of what language too.
    btw, Nope it's not selection sort, but that's not a bad guess

    That's alright tabstop, I was just winding you up I suppose.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double astrix pointer question..
    By transgalactic2 in forum C Programming
    Replies: 104
    Last Post: 01-19-2009, 05:02 AM
  2. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 02:53 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 07:59 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21