passing array to function problem

This is a discussion on passing array to function problem within the C++ Programming forums, part of the General Programming Boards category; hiya i have a function which i need to use an array of card objects that i made but it ...

  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
    21,658
    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)
    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

  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
    21,658
    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.
    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

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    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, 03:27 PM
  2. Passing a character array to a function
    By Qchem in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 06:18 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 6
    Last Post: 02-15-2005, 10: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

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