Thread: too many *'s

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    too many *'s

    ok i just want to double check this

    there is a function which reads out an array as

    Code:
    Func ((void **)&Array)
    but this function is in a library inside another function and i need to pass 'array' out

    So this would look like

    Code:
    int otherFunc((void **)Array){
            Func ((void **)&Array)
            ... 
            other stuff
            ...
            return 0;
    }
    Which I would call like:
    Code:
    otherFunc((void **)&Array)
    Is this right... i understand the idea of pointers and addresses but i dont like it when they get mixed with arrays.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Errm... what?

    What are the functions and variables declared as and what are you trying to do?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by chico1st View Post
    ok i just want to double check this

    there is a function which reads out an array as

    Code:
    Func ((void **)&Array)
    [/CODE]
    This is wrong. You're passing the address of the temporary representing the pointer to the array, and passing it to Func.

    You're also not following naming conventions.

    A function that takes a void ** should take an array of void pointers as its argument.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    Quote Originally Posted by robwhit View Post
    Errm... what?

    What are the functions and variables declared as and what are you trying to do?
    Func ((void **)&Array)
    Is a function that I have to use from a library which reads data from some hardware that I have

    All i want to do is pass on that data as a pointer since i need to access it from another program

    This function (otherFunc) will be part of a library which I need.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    doesn't Func have a return type?

  6. #6
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ok yes int Func

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    When you have an & in the prototype, that means a reference. And you don't use typecasts in the prototype ((void **)). But references are C++. This is the C board. Are you sure you have the prototype of the function and the right board?
    Last edited by robwhit; 05-20-2008 at 05:13 PM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think that's the call. Otherwise it might look like:
    Code:
    int Func (void *&Array)

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    ok, if it's the call, then what's the prototype?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >ok, if it's the call, then what's the prototype?
    Actually you raise an interesting question, because the following isn't even a valid function declaration:
    Code:
    int otherFunc((void **)Array){
    That what the original post looks like.

  11. #11
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    Quote Originally Posted by swoopy View Post
    >ok, if it's the call, then what's the prototype?
    Actually you raise an interesting question, because the following isn't even a valid function declaration:
    Code:
    int otherFunc((void **)Array){
    That what the original post looks like.
    So what am I supposed to do?
    i am hopelessly confuzed now.

    And I am supposed to be writing in C, if i did c++ by accident then thats bad

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you post the correct (compilable without errors) prototype of Func() - perhaps you can dig out the header file that has the prototype and post the ACTUAL function prototype.

    I wrote a bit of code like this:
    Code:
    int func(void **p);
    
    int main()
    {
       void *ptr;
       
       func(&ptr);
       return 0;
    }
    It compiles, but doesn't link since I don't have a "func" somewhere.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    Code:
    Int32 func(void** Array);
    if you were to be passing the value for ptr into main what would it look like?
    Code:
    int func(void **p);
    
    int main(*ptr)
    { 
       func(ptr);
       return 0;
    }
    OR
    Code:
    int func(void *p);
    
    int main(*ptr)
    { 
       func(&ptr);
       return 0;
    }
    Last edited by chico1st; 05-21-2008 at 08:24 AM.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Int32 func(void** Array);
    How is the array you want to pass to this function declared? Is it:
    Code:
    int *num_array;
    Or:
    Code:
    int **num_array;
    Or:
    Code:
    int num_array[10];
    Or:
    Code:
    int *num_array[10];
    Or is it actually a void **:
    Code:
    void **array;
    Or something else?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, one of those is right - it depends A LOT on what the function actually expects. It is impossible to say from just seeing the prototype of a function whether it expects the address of a void pointer [and whether it should be a void pointer that points to some valid memory, or if the function just sets the pointer to something within the function], or it expects the address of (for example) an array.

    Without either source of func, or the documentation of func, it's not possible for us to say what it does.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed