473,497 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I use 2 dimention pointer

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
}
Jul 23 '05 #1
3 1459
seamoon wrote in news:d0**********@news.hananet.net 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[i], 0, sizeof( szNameList[i] ) );
}
}
~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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 23 '05 #2
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)
{
}

Jul 23 '05 #3
Axter wrote:
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>.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1435
by: Slim | last post by:
I am trying to return a 2 dimensional array from a VB component, with no luck I get the array, it has the right Ubound for the first dimension, but 0 for the second. it works find when called...
4
2114
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
9806
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
2329
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
2852
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
2261
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
12882
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
2483
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
1
1374
by: vaidas gudas | last post by:
how to sort mul-dimention array by one dimention
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.