Thread: array decade pointers information

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    array decade pointers information

    Hi
    http://www.informit.com/guides/conte...eqNum=207&rl=1 here it writes:
    "Functions in C/C++ can't return or receive array arguments"
    Is this true?
    Can't we pass array to function as value? Is it always pointer?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    That's normally true, yes.

    Arrays are not passed by value, always as a pointer (or maybe as a reference in C++)

    But if you embed your array inside a struct, then that would be passed by value, if you passed the containing struct by value.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    When you pass an array as an argument, it decays to a pointer to its first element, so you are actually passing a pointer.
    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

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Thank you.
    Here there is an information about passing array with value to func.
    http://www.informit.com/guides/conte...eqNum=241&rl=1
    Code:
    template <typename T, size_t size>
    
    void foo(T (&array)[size])
    {
     //sort array, find the sum, min, max, etc
    }
    Is this right?
    Can we use like this? I mean is this really pasing array to func by value?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Seems right. If I understand templates right, size needs to be a compile-time constant, though.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This demonstrates the same thing. It may be easier to understand.
    Code:
    void foo(int (&array)[size]) {
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Does it? I get size is undefined? The trick with the template is that size is a compile time constant.

    Your approach would need magic constants both in the function and in the calling code, or the use of a global constant, define or something like that.

    I'll probably need to check the link, because I don't get why and where this would be useful...

    Edit: back. This is just a proof of concept...
    Last edited by anon; 05-02-2007 at 01:01 PM.

  8. #8
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    why pass array ?? as some said, you pass pointer to that array and then return the pointer
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, the aim is that size is deducted from the passed array, so you don't need to pass it separately.

    However, this could be an example of "clever code", or a clever way to shoot yourself in the leg (the array has already decayed to pointer).

    Any C++ programmer would consider standard containers first

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  4. Loading an array using pointers
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2001, 05:23 AM
  5. using information from an array
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 05:30 PM