943,587 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 456
  • C++ RSS
Sep 13th, 2009
0

Array question

Expand Post »
is there a short hand way of passing just a row in a 2d array into a function that accepts only 1d arrays. for example something like this even though I know that this doesn't work. I don't want to have to use loops to copy a row from the 2d array into a 1d array and then pass the 1d array into the function foo.

C++ Syntax (Toggle Plain Text)
  1. function foo(int array[4]) {
  2. do something
  3. }
  4.  
  5. int main() {
  6. int test[2][4] = {{1,2,3,4},{5,6,7,8}};
  7.  
  8. // pass only 1,2,3,4 into the function
  9. foo(test[0][]);
  10. }
Last edited by Talguy; Sep 13th, 2009 at 10:51 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Talguy is offline Offline
96 posts
since Mar 2009
Sep 13th, 2009
0

Re: Array question

That's an interesting idea TalGuy. I havn't written any code to test it, but one thing about arrays is the data is stored sequentially. Perhaps write the function to accept a pointer to the data type and the number of records to process, then pass the starting address for the row you wish it to process? Just a thought.
Reputation Points: 244
Solved Threads: 31
Posting Whiz
Frederick2 is offline Offline
330 posts
since Jul 2008
Sep 13th, 2009
1

Re: Array question

when passing the row from a 2D array to a function that takes a 1D parameter, use only the row index in your argument.
C++ Syntax (Toggle Plain Text)
  1. foo( test[0] );
That essentially passes a pointer to where that row begins.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 13th, 2009
0

Re: Array question

Just out of idle curiosity I just tested it. Pretty much as I thought. You see, if you have a two dimensional array like test[2][4], then this...

test[1]

will represent the address of the second row. So yes, you can define a function taking a one dimensional array as a parameter (plus a count integer), and have it operate on any row which you pass to it from a two dimensional array. And I'm sure this is a general case true with arrays of higher dimension. I got it working in my test code anyway. Try this...

(piles of warnings you'll have to clean up, unfortunately)
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int Sum1(int iArr[], int n)
  4. {
  5. int iTot=0;
  6.  
  7. for(int i=0;i<n;i++)
  8. iTot=iTot+iArr[i];
  9.  
  10. return iTot;
  11. }
  12.  
  13. int Sum(int* pInt, int n)
  14. {
  15. int iTot=0;
  16.  
  17. for(int i=0;i<n;i++)
  18. iTot=iTot+pInt[i];
  19.  
  20. return iTot;
  21. }
  22.  
  23.  
  24. int main(void)
  25. {
  26. int i,j;
  27.  
  28. int test[2][4] = {{1,2,3,4},{5,6,7,8}};
  29. printf("Sum=%d\n\n",Sum(&test[1][0],4));
  30. for(i=0;i<2;i++)
  31. for(j=0;j<4;j++)
  32. printf("%u\t\t%d\n",&test[i][j],test[i][j]);
  33. printf("\n");
  34. printf("%u\n",test[1]);
  35. printf("Sum1=%u\n",Sum1(test[1],4));
  36.  
  37. return 0;
  38. }
Reputation Points: 244
Solved Threads: 31
Posting Whiz
Frederick2 is offline Offline
330 posts
since Jul 2008
Sep 14th, 2009
0

Re: Array question

Thanks vmanes, thats exactly how I needed to do it.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Talguy is offline Offline
96 posts
since Mar 2009

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: Cause effect analysis!
Next Thread in C++ Forum Timeline: Finalizing File for distribution?





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


Follow us on Twitter


© 2011 DaniWeb® LLC