function arguments and pointers revisited

Discussion in 'C Programming' started by Sheldon, Nov 7, 2007.

  1. Sheldon

    Sheldon Guest

    Hi Everybody,

    I am having trouble with a fairly simple problem, I am sure.

    I have a structure that contains 2D arrays:

    struct S {
    float array[4][4];
    .....
    snip
    .....
    } *ptr;

    and a function: int Trans(float data_array) {

    int i, j, jj;
    float array[16];

    for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
    array[jj] = data_array[j];
    }
    }
    return 1;
    }

    I want to pass the array to the function like this:
    res = Trans(ptr->array);

    For brevity, I have not included any error checking in the call.

    This way gives me a pointer type error and I don't want to send the
    entire structure to the function.

    Does anyone how to quickly do this?

    Appreciate your help.
    /S
     
    Sheldon, Nov 7, 2007
    #1
    1. Advertisements

  2. Sheldon

    dj3vande Guest



    <http://www.c-faq.com/aryptr/pass2dary.html>
    (Short answer: "int Trans(float data_array[][4])")

    Whether this is in fact the best way to do it may depend on what you're
    Really Trying To Do.
    If the reason you don't want to pass the entire structure to the
    function is because you don't want to have to copy a bunch of data
    around, all you need to pass is a pointer:
    --------
    int trans(struct S *data)
    {
    /*...*/
    array[jj]=data->array[j];
    /*...*/
    }
    --------
    The struct itself doesn't need to be copied; it stays where it is and
    the callee accesses it through the pointer. (But this won't help you
    much if the reason you don't want to pass the entire structure is
    because it doesn't make sense to do so.)


    dave
     
    dj3vande, Nov 7, 2007
    #2
    1. Advertisements

  3. Sheldon

    Eric Sosman Guest

    Sheldon wrote On 11/07/07 11:44,:
    ITYM `float data_array[4][4]'.
     
    Eric Sosman, Nov 7, 2007
    #3
  4. Sheldon

    John Bode Guest

    This prototype expects data_array to be a single float, not a 2-d
    array of float.

    Remember that when you pass an array as an argument, what actually
    gets passed is a pointer to the first element of the array. Let's
    start with a 1-d example:

    float array[4];

    The type of "array" is "4-element array of float". In most contexts
    (such as passing the array as a parameter to a function), the type is
    implicitly converted to "pointer to float", and what actually gets
    passed is a pointer to the first array element:

    void func(float *array) {...}
    int main(void)
    {
    float array[4];
    ...
    func(array);
    ...
    }

    Now let's look at a 2-d example:

    float array[4][4];

    the type of "array" is "4-element array of 4-element array of float".
    When passed to a function, the type is converted to "pointer to a 4-
    element array of float", so your prototype needs to be

    int trans(float (*data_array)[4])
    {
    }

    Honestly, you save yourself some headaches by passing a pointer to the
    whole struct, rather than trying to pass a multi-dimensional array
    object.
     
    John Bode, Nov 7, 2007
    #4
  5. Sheldon

    Sheldon Guest

    You are right, it would be much easier to send in the entire struct
    but that would require rewriting someone else's code and I don't have
    time to do that. I tried prototype as you mentioned and it works. I
    appreciate the help everyone. Thanks a lot!

    /S :)
     
    Sheldon, Nov 7, 2007
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.