How can I use 2 dimention pointer

S

seamoon

How can I use 2 dimension of array instead of using pointer
array, in the following exmples.

I treated pointer variable in SSetDlg structuer as [n][50]
array. But I think that there's an exact method to use the
2 dimension pointer array in the SSetDlg structure.
For example, the same as like struct SSetDlg { char *c[]; }.
Give me your advice please.

struct SSetDlg
{
char *szNameList; // [n][50], -> 2 dimension array
};

void CSetDlg::OnSet()
{
// TODO: Add your control notification handler code here
struct SSetDlg stSetDlg;

int n = m_list.GetItemCount();

stSetDlg.szNameList = new char [(n+1)*50]; // [n+1][50] allocate

memset(stSetDlg.szNameList, 0, ((n+1)*50)); // [n+1][50] initialize

for (int nItem = 0; nItem < n; nItem++)
{
m_list.GetItemText(nItem, 0, // save [n][50] characters in list
&stSetDlg.szNameList[nItem*50], 50); // to an pointer ( [n][50] array)
}

delete stSetDlg.szNameList; // [n+1][50] free
}
 
R

Rob Williscroft

seamoon wrote in in comp.lang.c++:
How can I use 2 dimension of array instead of using pointer
array, in the following exmples.

I treated pointer variable in SSetDlg structuer as [n][50]
array. But I think that there's an exact method to use the
2 dimension pointer array in the SSetDlg structure.
For example, the same as like struct SSetDlg { char *c[]; }.
Give me your advice please.

#include <iostream>
#include <ostream>
#include <cstring>


struct SSetDlg
{
char (* const szNameList)[ 50 ];

SSetDlg( std::size_t n ) : szNameList( new char[n][50] )
{
for ( std::size_t i = 0; i < n; ++i )
{
std::memset( szNameList, 0, sizeof( szNameList ) );
}
}
~SSetDlg()
{
delete [] szNameList;
}

private:
SSetDlg( SSetDlg const & ); /* prevent copying */
};

void CSetDlg_OnSet()
{
int n = 10;

SSetDlg stSetDlg( n );

for ( std::size_t i = 0; i < n; ++i )
{
std::strncpy(
stSetDlg.szNameList[ i ], "sample", sizeof( stSetDlg.szNameList[0] )
);
}

for ( std::size_t i = 0; i < n; ++i )
{
std::cout << stSetDlg.szNameList[ i ] << '\n';
}
std::cout.flush();
}

int main()
{
CSetDlg_OnSet();
}


Hopefully the above answers your question about array's. The
parenthesis are required as otherwise you get an array of 50
pointers not a pointer to an array of 50 char.

Here's a different approach using std::vector<>, it uses more free
store (heap) but that shouldn't matter here.

It has the advantage that SSetDlg is now coypable and assignable plus
there is nolonger any need to manage the new [] and delete [] calls.

#include <iostream>
#include <ostream>
#include <vector>

struct SSetDlg
{
std::vector< std::vector<char> > szNameList;

SSetDlg( std::size_t n ) : szNameList( n, std::vector<char>(50) )
{
}
};

void CSetDlg_OnSet()
{
int n = 10;

SSetDlg stSetDlg( n );

for ( std::size_t i = 0; i < n; ++i )
{
std::strncpy(
&stSetDlg.szNameList[ i ][0], "sample",
stSetDlg.szNameList[ i ].size()
);
}

for ( std::size_t i = 0; i < n; ++i )
{
std::cout << &stSetDlg.szNameList[ i ][0] << '\n';
}
std::cout.flush();
}

int main()
{
CSetDlg_OnSet();
}

Rob.
 
A

Axter

Check out the following link for multiple methods for creating 2
dimensional arrays.

http://www.tek-tips.com/faqs.cfm?fid=5575

I recommend you use a poitner of pointers:
char **szNameList;

Using function in above code, you can create it like this:

SSetDlg::SSetDlg( std::size_t n ) : szNameList(
Allocate2DArray<char>(n, 50))
{
}

or

SSetDlg::SSetDlg( int x, int y) : szNameList( Allocate2DArray<char>(x,
y))
{
}

http://code.axter.com/dynamic_2d_array.h
You can also use the dynamic_2d_array class posted in above link.
dynamic_2d_array<char> szNameList;
SSetDlg( int x, int y) : szNameList( x, y)
{
}
 
R

red floyd

Axter said:
Check out the following link for multiple methods for creating 2
dimensional arrays.

http://www.tek-tips.com/faqs.cfm?fid=5575

I recommend you use a poitner of pointers:
char **szNameList;

Using function in above code, you can create it like this:

SSetDlg::SSetDlg( std::size_t n ) : szNameList(
Allocate2DArray<char>(n, 50))
{
}

or

SSetDlg::SSetDlg( int x, int y) : szNameList( Allocate2DArray<char>(x,
y))
{
}

http://code.axter.com/dynamic_2d_array.h
You can also use the dynamic_2d_array class posted in above link.
dynamic_2d_array<char> szNameList;
SSetDlg( int x, int y) : szNameList( x, y)
{
}

Since what you're really looking for is an array of strings, I'd use a
vector of strings, i.e. std::vector<std::string>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top