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

  2. Sheldon

    Guest

    In article <>,
    Sheldon <> wrote:
    >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?


    <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
     
    , Nov 7, 2007
    #2
    1. Advertising

  3. Sheldon

    Eric Sosman Guest

    Sheldon wrote On 11/07/07 11:44,:
    > 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) {


    ITYM `float data_array[4][4]'.

    --
     
    Eric Sosman, Nov 7, 2007
    #3
  4. Sheldon

    John Bode Guest

    On Nov 7, 10:44 am, Sheldon <> wrote:
    > 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) {


    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

    On 7 Nov, 18:56, John Bode <> wrote:
    > On Nov 7, 10:44 am, Sheldon <> wrote:
    >
    > > 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) {

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


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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Elliott
    Replies:
    5
    Views:
    321
    Bo Persson
    Jan 14, 2006
  2. claudiu
    Replies:
    12
    Views:
    4,472
    Gianni Mariani
    Aug 6, 2007
  3. jmborr
    Replies:
    1
    Views:
    453
    Stargaming
    Nov 3, 2007
  4. Jonathan Wood

    Passing Arguments to Web Forms (Revisited)

    Jonathan Wood, Feb 11, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    309
    Jonathan Wood
    Feb 11, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    729
Loading...

Share This Page