how do i pass an array to a function .. and also return an array back from the func????

Discussion in 'C Programming' started by bhejafry.mmk@gmail.com, Nov 30, 2006.

  1. Guest

    hey ..
    i've been tryin this for a long time now ..
    jow the bloody hell do i pass an array to the function ..
    also .. how do i get the function to return an array ...
    all in C language ...
    please help
     
    , Nov 30, 2006
    #1
    1. Advertising

  2. Chris Dollin Guest

    wrote:

    > hey ..
    > i've been tryin this for a long time now ..


    How long? Minutes? Hours? Days? Weeks?

    Longer than it would take to find a decent book on C and
    read it? (I think K&R2 is pretty good, myself.)

    > jow the bloody hell do i pass an array to the function ..


    That depends what you mean by that, but one interpretation
    allows you to just write the array name in the argument
    position. Did you try that? What happened? Why didn't you
    post your trial code -- it's much easier to correct
    something than to offer advice in a vacuum.

    > also .. how do i get the function to return an array ...


    Strictly, you can't. But you can return a /pointer/, usually
    to the first element of the array. You also need a way of
    saying how big the array thingy is. There are other tricks.
    Which ones are best depends rather on exactly what you're
    trying to do.

    --
    Chris "subtle, like a barrel" Dollin
    The "good old days" used to be much better.
     
    Chris Dollin, Nov 30, 2006
    #2
    1. Advertising

  3. Re: how do i pass an array to a function .. and also return anarray back from the func????

    writes:
    > i've been tryin this for a long time now ..
    > jow the bloody hell do i pass an array to the function ..
    > also .. how do i get the function to return an array ...
    > all in C language ...


    Reading the comp.lang.c FAQ would be a good start.
    <http://www.c-faq.com>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Nov 30, 2006
    #3
  4. :

    > hey ..
    > i've been tryin this for a long time now ..
    > jow the bloody hell do i pass an array to the function ..
    > also .. how do i get the function to return an array ...
    > all in C language ...
    > please help



    Array of any length? You'd need pointers:

    int *Func(int *p)
    {
    p[0] = 6;
    p[1] = 7;
    p[2] = 2;

    return p;
    }

    Array of constant length? You can use the method above, or:

    int (*Func(int (*const parr)[len]))[len]
    {
    int *p = *parr;

    p[0] = 6;
    p[1] = 7;
    p[2] = 2;

    return parr;
    }

    or, if you'd like to wrap it in a struct:

    struct ArrFiveInts {
    int arr[5];
    };

    ArrFiveInts *Func(ArrFiveInts *p);

    If you pass an ArrFiveInts by value, then you'll have a copy of the array
    in the called function.

    --

    Frederick Gotham
     
    Frederick Gotham, Nov 30, 2006
    #4
    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. CRON
    Replies:
    24
    Views:
    203,880
    Adrienne Boswell
    Jun 20, 2006
  2. Johnny
    Replies:
    3
    Views:
    484
    Robert Kern
    Aug 23, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    559
    Hari Sekhon
    Jun 20, 2006
  4. Vinko Vrsalovic

    int func() v/s int func(void)

    Vinko Vrsalovic, Jan 21, 2005, in forum: C Programming
    Replies:
    14
    Views:
    1,373
    Villy Kruse
    Jan 24, 2005
  5. Alex Vinokur
    Replies:
    6
    Views:
    387
    Tor Rustad
    Nov 18, 2006
Loading...

Share This Page