943,759 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3921
  • C++ RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Sep 6th, 2007
1

Re: passing arrays / updating object arrays

C++ Syntax (Toggle Plain Text)
  1. toppings[z] = a[z] ;
If a is declared as a plain int, why are you subscripting it like an array of int? I think you should make the whole thing an array and that'll fix your problem.
C++ Syntax (Toggle Plain Text)
  1. void Pizza::set ( int x , int y , int a[] , int i );
C++ Syntax (Toggle Plain Text)
  1. void Pizza::set ( int x , int y , int a[] , int i )
  2. {
  3. type = x ;
  4. size = y ;
  5. for ( int z = 0 ; z < i ; z++ )
  6. toppings[z] = a[z] ;
  7. }
C++ Syntax (Toggle Plain Text)
  1. pizza.set ( x , y , a, i ) ;
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Sep 6th, 2007
0

Re: passing arrays / updating object arrays

awesome!! It's working now!
Now I just need to do some slight modifications and add the computePrice function and I'm done.

thanks everyone!!
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Sep 6th, 2007
0

Re: passing arrays / updating object arrays

I thought you had declared the third parameter as a reference to an array?
C++ Syntax (Toggle Plain Text)
  1. void Pizza::set ( int x , int y , int (&a)[7] , int i )
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 6th, 2007
0

Re: passing arrays / updating object arrays

i did but then i found out that c++ passes all arrays by reference by default.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Sep 6th, 2007
0

Re: passing arrays / updating object arrays

Quote ...
i did but then i found out that c++ passes all arrays by reference by default.
You have to be careful here. 'By reference' could mean two things, and only one of them is right. Thef irst by reference is an actual reference in C++ and arrays are not passed by that reference by default. To pass an array by actual reference in C++ you do it like Dave said.
C++ Syntax (Toggle Plain Text)
  1. void Pizza::set ( int x , int y , int (&a)[7] , int i )
The second by reference is a fake reference that you get by passing a pointer by value and being able to get to the thing that the pointer points to. That's the default for arrays and that's why all of these are the same prototype.
C++ Syntax (Toggle Plain Text)
  1. void Pizza::set ( int x , int y , int a[7] , int i )
  2. void Pizza::set ( int x , int y , int a[] , int i )
  3. void Pizza::set ( int x , int y , int *a , int i )
Woohoo! Finally something I know something about.
Last edited by Hamrick; Sep 6th, 2007 at 10:20 pm.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Sep 7th, 2007
0

Re: passing arrays / updating object arrays

ah ok. so did i create a pointer?
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Sep 7th, 2007
0

Re: passing arrays / updating object arrays

The answer depends on the current code.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 7th, 2007
0

Re: passing arrays / updating object arrays

it's the same as post #31.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Sep 7th, 2007
0

Re: passing arrays / updating object arrays

That's a pointer (see #35).
http://c-faq.com/aryptr/aryptrparam.html
Last edited by Dave Sinkula; Sep 7th, 2007 at 12:33 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pls Help
Next Thread in C++ Forum Timeline: Dynamic array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC