Thread: passing array to function problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    passing array to function problem

    hiya i have a function which i need to use an array of card objects that i made but it says
    error C2234: 'thand' : arrays of references are illegal
    i have the same error for shand
    here is my fn
    Code:
    void copytosplit(card& thand[],card& shand[],int index)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you pass an array, it will be converted to a pointer anyway, so just remove the & to indicate that it's a reference (a reference is almost the same as a pointer, but not quite).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    so if i remove the'&' and pass the array it will still change the value after the function has finished?
    or will it be the same as passing by value?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by thestien View Post
    so if i remove the'&' and pass the array it will still change the value after the function has finished?
    or will it be the same as passing by value?
    It will pass the array as a pointer to the array, so you can change the content of the array and the changes will persist after the function returns, because all that is passed is the location of where the original array is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    so if i remove the'&' and pass the array it will still change the value after the function has finished?
    Yes, since arrays are converted to pointers to their first element. What you would not be able to change is the array itself, but if you are passing a fixed size array, you cannot change the array itself anyway.

    If you want to pass pointers to dynamically allocated arrays and have the function allocate the arrays, then you should use:
    Code:
    void copytosplit(card*& thand, card*& shand, int index)
    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

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    thanks guys thats really helpful

    i have a fixed size array that would be passed to the function and need it to change the value.
    but know i know it passes them as pointers anyway then that makes life easier
    so thanks alot for the help.
    eventually im going to change the arrays to vectors once ive learned how to use them would passing a vector be the same as passing an array?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    eventually im going to change the arrays to vectors once ive learned how to use them would passing a vector be the same as passing an array?
    Not quite, since you would pass it by reference or const reference instead of by value.
    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

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A vector isn't an array and they therefore behave differently. A vector is an object, so no pointer to its first element is passed. The entire object itself is passed, so it works just like any other non-array type. You simply pass it by reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    oh super thats good to hear one day ill learn how to use pointers properly then i wont need to pass by reference then right?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++ devs always prefer passing by reference if possible. I'd suggest you learn both things, because references are so much easier than pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    oh ok thanks for the advice its just in a book i got it says its better to pass by pointer but i must admit it sure is easy to pass most things by ref

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The book is wrong. I would say it's far better to pass by reference, if possible (as would many others I think).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ok cool ty ill keep that in mind

    i seem to have another problem now
    Code:
    if (checksplit(a.myHand[])==true)
    { some other code here
    i get 2 errors
    : error C2059: syntax error : ']'
    error C2143: syntax error : missing ';' before '{'
    anyidea how to fix that?
    oh by the way this function is called inside another function could that be the problem?

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You probably need to call it like this:
    Code:
    if (checksplit(a.myHand)==true)
    { some other code here
    Operator[] is used to access items, not to tell the compiler what a.myHand is. The compiler already knows what it is.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  2. Passing a character array to a function
    By Qchem in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 07:18 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM