[英]How to add data dynamically in structure which is a Pointer to an array of pointers
I have two structure as below: 我有两个结构如下:
typdef struct abc
{
int id;
char name;
}s_abc,*lpabc;
typdef struct result
{
int acc_no;
lpabc *details;
}s_res;
I need to dynamically add the data within the structure result which points out to an array of pointers ie: struct abc
The structure abc
could be an array of 5 values for eg. 我需要在结构结果内动态添加数据,该结果指向一个指针数组,即:
struct abc
结构abc
可以是5个值的数组,例如。 How should i add the values ? 我应该如何添加值?
the structure defined are explicit: For better understanding i'm attaching the structure below:- 定义的结构是明确的:为了更好地理解,我附上以下结构:-
typedef struct _wfs_cdm_physicalcu
{
LPSTR lpPhysicalPositionName;
CHAR cUnitID[5];
ULONG ulInitialCount;
ULONG ulCount;
ULONG ulRejectCount;
ULONG ulMaximum;
USHORT usPStatus;
BOOL bHardwareSensor;
} WFSCDMPHCU, *LPWFSCDMPHCU;
typedef struct _wfs_cdm_cashunit
{
USHORT usNumber;
USHORT usType;
LPSTR lpszCashUnitName;
CHAR cUnitID[5];
CHAR cCurrencyID[3];
BOOL bAppLock;
USHORT usStatus;
USHORT usNumPhysicalCUs;
LPWFSCDMPHCU *lppPhysical;
} WFSCDMCASHUNIT, *LPWFSCDMCASHUNIT;
typedef struct _wfs_cdm_cu_info
{
USHORT usTellerID;
USHORT usCount;
LPWFSCDMCASHUNIT *lppList;
} WFSCDMCUINFO, *LPWFSCDMCUINFO;
Here i need to access the data of _wfs_cdm_physicalcu 4 times ie : an array. 在这里,我需要访问_wfs_cdm_physicalcu的数据4次,即:数组。
Stop using C idioms in C++; 停止在C ++中使用C习语; that only leads to confusion.
只会导致混乱。
#include <string>
#include <vector>
struct abc {
int id;
std::string name;
};
struct result {
int acc_no;
std::vector<abc> details;
};
Now you can easily add as many abc
values to the array as you like: 现在,您可以轻松地向数组中添加任意多个
abc
值:
result r {42, {{1, "Mike"}, {2, "Fred"}}}; // inialise with two values
r.details.emplace_back(3, "Mary"); // add a third
I think if you want to use malloc, you should define a pointer that points to the struct abc. 我认为,如果要使用malloc,则应定义一个指向结构abc的指针。 Then if you find a new one you can malloc new memory and access it via the pointer.
然后,如果找到新的内存,则可以malloc新的内存并通过指针访问它。 I also think the STL vector will be a better choice.
我也认为STL向量将是更好的选择。 It is very convenient.
非常方便。 You can learn some knowledge about STL.
您可以学习有关STL的一些知识。 That's so interesting.
真有趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.