Archived

This topic is now archived and is closed to further replies.

Da cobra

passing a dubble array to a function

Recommended Posts

Da cobra    122
how do you pass on a dubble array like this char array[2] [80] = {{"text 1"}, {"text2}} ; function_text(char) ; // <= I want to pass the whole array to this function and no pointers pls thanx in advance for any help...

Share this post


Link to post
Share on other sites
reza    122



void FuntTakingA2DArray(char arr[2][80])
{
cout<< arr[0];
cout<< arr[1];
}

void main()
{
char arr[2][80]={{"text1"},{"text2"}};
FuntTakingA2DArray(arr);
}



the name of an array contains the base address of the array which we are passing to the function . As u mentioned no pointers happy!

Share this post


Link to post
Share on other sites
Da cobra    122
thanx for the quick answer but I forgot to say something :

what if I don''t know how big the array will be?

it could also be :

char array[2] [80] = {{"text 1"}, {"text2}, {"text 3"}} ;

again thanx in advance for any replies

Share this post


Link to post
Share on other sites
Zahlman    1682
If you don''t know the sizes, you''ll have to use pointers. Or a language which is a little fancier about its array implementation, such as Java. Or perhaps some sort of library. Sorry, I don''t remember how to do it in C++, or C for that matter, offhand - I pretty much never had to deal with such arrays in my code.

In that last example, you''ll need to use [3][80] rather than [2][80], of course.

It''s spelled "double", and you''re talking about a 2-dimensional array, not a "double array" which one would normally understand to mean an array of doubles (double-precision floating-point values).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
if it''s an array of null terminated strings you could do this:

func(const char* array[], int size){
for(int i=0; i<size; i++){
cout << array;
}
}

or else i think you have to pass both dimensions to the function?

Share this post


Link to post
Share on other sites