| | |
Array question
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 58
Reputation:
Solved Threads: 0
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)
function foo(int array[4]) { do something } int main() { int test[2][4] = {{1,2,3,4},{5,6,7,8}}; // pass only 1,2,3,4 into the function foo(test[0][]); }
Last edited by Talguy; Sep 13th, 2009 at 10:51 pm.
•
•
Join Date: Jul 2008
Posts: 169
Reputation:
Solved Threads: 20
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.
when passing the row from a 2D array to a function that takes a 1D parameter, use only the row index in your argument.
That essentially passes a pointer to where that row begins.
C++ Syntax (Toggle Plain Text)
foo( test[0] );
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jul 2008
Posts: 169
Reputation:
Solved Threads: 20
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)
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)
#include <stdio.h> int Sum1(int iArr[], int n) { int iTot=0; for(int i=0;i<n;i++) iTot=iTot+iArr[i]; return iTot; } int Sum(int* pInt, int n) { int iTot=0; for(int i=0;i<n;i++) iTot=iTot+pInt[i]; return iTot; } int main(void) { int i,j; int test[2][4] = {{1,2,3,4},{5,6,7,8}}; printf("Sum=%d\n\n",Sum(&test[1][0],4)); for(i=0;i<2;i++) for(j=0;j<4;j++) printf("%u\t\t%d\n",&test[i][j],test[i][j]); printf("\n"); printf("%u\n",test[1]); printf("Sum1=%u\n",Sum1(test[1],4)); return 0; }
![]() |
Similar Threads
- Little Simple array question :) (C)
- Simple array question (C++)
- Simple char array question (C)
- Newbie Constructor / Array question (C++)
- Array Question (C++)
- C++ Array Question (C++)
- array question (C++)
Other Threads in the C++ Forum
- Previous Thread: Cause effect analysis!
- Next Thread: Finalizing File for distribution?
Views: 251 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






