Click here to Skip to main content
15,848,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
i have to pass a TCHAR 2D array by reference through a function.
Eg:
C++
Function1(tcStringList); // calling function, tcStringList is 2D array of 3 strings, each of size allocated to "MAX_LENGTH".

//function body
Function1(TCHAR* tcStringList[][MAX_LENGTH])// Is this syntax correct?
{
// here do memcopy and store values to tcStringList
}


I am not getting the syntax of how to pass the 2D TCHAR array.


Can anyone provide me the solution?
Posted
Updated 30-Jun-10 21:56pm
v2

Try this - Function1(TCHAR* tcStringList[3])
 
Share this answer
 
i inserted like you said. but it returned error.. cannot convert parameter 1 from 'TCHAR [3][32]' to 'TCHAR *[]
error was in calling function.

Befor calling the function, i have initialized the 2D array as below:-
TCHAR tcSringList[3][32]={_T("\0")};
Function1(tcStringList);
 
Share this answer
 
C++
TCHAR tcStringList[3][32]={_T("\0")};


void Function1( TCHAR tcStringList[][32])
{
  // here do memcopy and store values to tcStringList
}


:)
 
Share this answer
 
now its working.. thanks CPallini.. :)
 
Share this answer
 
As it says you're using C++ then I'd use:

void do_something( TCHAR (&a)[ 3 ][ 32 ] )
{
}

over

void do_something( TCHAR a[][ 32 ] )
{
}

or

void do_something( TCHAR (*a)[32] )
{
}

as this avoids the C style decay of arrays to pointers. It won't stop you blowing the array bounds but it means you know the size of the array inside the function and write safer code.

As an alternative instead of using a 2D array why not use a vector of strings?

void do_something( std::vector<std::basic_string<TCHAR>> &strings )
{
}


It's a bit easier to use and you don't have to muck about with low level guff like pointers and built in array. Or as you've called the thing a string list would a list be more appropriate?

void do_something( std::list<std::basic_string<TCHAR>> &string_list )
{
}

Cheers,

Ash
 
Share this answer
 
Thanks for the information.. i am a newbie in C++.. :)
Regarding my application(which is a dll), the functions must be of C interface type. ie, the function can be imported to C also.. if i use std::vector or std::list, will it work for a C interface?
 
Share this answer
 
Comments
Aescleal 1-Jul-10 6:45am    
In that case you either of the second two definitions for the function, they're C compatible. While there are ways to pass C++ constructs through layers of C they're not pretty and not generally worth doing so avoid using anything from the C++ standard library in your interfaces.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900