Thread: Passing as argument a part of a table

  1. #1
    Registered User
    Join Date
    Apr 2007

    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

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, 09: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