Array question

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 58
Reputation: Talguy is an unknown quantity at this point 
Solved Threads: 0
Talguy Talguy is offline Offline
Junior Poster in Training

Array question

 
0
  #1
Sep 13th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 169
Reputation: Frederick2 has a spectacular aura about Frederick2 has a spectacular aura about Frederick2 has a spectacular aura about 
Solved Threads: 20
Frederick2 Frederick2 is offline Offline
Junior Poster

Re: Array question

 
0
  #2
Sep 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Array question

 
1
  #3
Sep 13th, 2009
when passing the row from a 2D array to a function that takes a 1D parameter, use only the row index in your argument.
  1. foo( test[0] );
That essentially passes a pointer to where that row begins.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 169
Reputation: Frederick2 has a spectacular aura about Frederick2 has a spectacular aura about Frederick2 has a spectacular aura about 
Solved Threads: 20
Frederick2 Frederick2 is offline Offline
Junior Poster

Re: Array question

 
0
  #4
Sep 13th, 2009
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)
  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 58
Reputation: Talguy is an unknown quantity at this point 
Solved Threads: 0
Talguy Talguy is offline Offline
Junior Poster in Training

Re: Array question

 
0
  #5
Sep 14th, 2009
Thanks vmanes, thats exactly how I needed to do it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 251 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC