Passing as argument a part of a table

This is a discussion on Passing as argument a part of a table within the C++ Programming forums, part of the General Programming Boards category; I want to pass as argument a part of an array. I have thought to create a pointer of the ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52

    Passing as argument a part of a table

    I want to pass as argument a part of an array. I have thought to create a pointer of the same type as the array and pass the pointer instead of the array. Is this right? There is any better solution to my problem?

    Second solution (I just thought it): Can I pass the address of the first element + the sizeof(element) * (the number of the elements I want to omit)?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    You can pass two pointers to denote the start and end of the range within the array. The common idiom in the C++ standard library is to have the end pointer point to one past the actual last element in the range.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    myFunc ( &myArray[someIndex] );

    Or if you want the start of the array

    myFunc ( &myArray[0] );

    Which is more concisely written as

    myFunc ( myArray );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-15-2008, 02:16 AM
  2. Replies: 2
    Last Post: 04-11-2008, 02:42 AM
  3. Passing a function as an argument
    By Xzyx987X in forum C Programming
    Replies: 10
    Last Post: 04-20-2004, 10:32 PM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM
  5. Passing a double array to a function as an argument
    By Glirk Dient in forum C++ Programming
    Replies: 20
    Last Post: 09-10-2003, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21