ARRAYS AND FUNCTIONS

Discussion in 'C++' started by coinjo, Dec 5, 2005.

  1. coinjo

    coinjo Guest

    How to pass a full array into a function?
     
    coinjo, Dec 5, 2005
    #1
    1. Advertising

  2. coinjo wrote:
    > How to pass a full array into a function?
    >


    What do you mean? Idiomatic approach is to pass a pointer to the first
    element of the array to the function. Does it not work for you? Read
    the FAQ 5.8. Arrays do not have copy semantics defined for them alone,
    only if they are part of a struct. You can wrap your array in a struct
    and pass that struct by value. But my question would be, 'why?'

    V
     
    Victor Bazarov, Dec 5, 2005
    #2
    1. Advertising

  3. coinjo wrote:
    > How to pass a full array into a function?


    array name gets converted to a pointer to the first element when passed
    to a function. You can very well write

    int f(int a[])
    {
    // your code here
    }

    But this is just a syntactic sugar for
    int f(int *a)
    Arrays cannot be passed by value.

    Also, avoid arrays. Use vector.
     
    Neelesh Bodas, Dec 5, 2005
    #3
  4. coinjo

    coinjo Guest

    Ok. Tell me how to pass a pointer to the first element of the array to
    the function?
     
    coinjo, Dec 5, 2005
    #4
  5. coinjo

    Rolf Magnus Guest

    coinjo wrote:

    > How to pass a full array into a function?


    You can't directly pass an array to a function. You can pass a pointer to
    its first element, or a reference to the array, or - if you want it to be
    copied - a struct that contains the array as member.
    Alternatively, simply use std::vector.
     
    Rolf Magnus, Dec 5, 2005
    #5
  6. coinjo

    coinjo Guest

    I can't avoid them cause i HAVE to use them!
     
    coinjo, Dec 5, 2005
    #6
  7. coinjo wrote:
    > Ok. Tell me how to pass a pointer to the first element of the array to
    > the function?


    "Tell me how to pass a pointer to the first element of the array to
    the function, PLEASE".

    void foo(int *ptr)
    {
    }

    int main()
    {
    int array[100];
    foo(array); // that's how
    }

    V
     
    Victor Bazarov, Dec 5, 2005
    #7
  8. coinjo wrote:
    > I can't avoid them cause i HAVE to use them!
    >


    "Avoid them"? Whom? Functions or arrays? Why are you
    responding to _your_own_ post, in which you didn't even
    use the word "avoid"?
     
    Victor Bazarov, Dec 5, 2005
    #8
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,621
    Victor Bazarov
    Jun 21, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,268
    Bob Hairgrove
    Jun 22, 2004
  3. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    387
  4. Pete
    Replies:
    10
    Views:
    560
    John F. Bode
    Sep 22, 2004
  5. Philipp
    Replies:
    21
    Views:
    1,140
    Philipp
    Jan 20, 2009
Loading...

Share This Page