0

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. 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.

4
  • 5
    Since this is C++ I would suggest you use a vector instead of dynamic arrays. Commented May 8, 2015 at 12:04
  • Why is it pointer to pointer at first place. Consider changing it to just pointer or some C++ container unless required explicitly by design. Commented May 8, 2015 at 12:05
  • 1
    I completely don't understand what you are trying to do... details is a pointer to an array and you want to have many abcs? Or do you want to change number of fields in abc? This is completely confusing lpabc *details; why are you creating pointer to a pointer? Maybe try post code with semantics and syntax you are trying to achieve. Commented May 8, 2015 at 12:11
  • @MohitJain :,I have posted the syntax for ref. Commented May 8, 2015 at 12:30

2 Answers 2

5

Stop using C idioms in 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:

result r {42, {{1, "Mike"}, {2, "Fred"}}};  // inialise with two values
r.details.emplace_back(3, "Mary");         // add a third
Sign up to request clarification or add additional context in comments.

9 Comments

@TechBrkTru: You don't, you access them through the vector. r.details[1].name == "Fred".
Well in my structure... the first two values of struct _wfs_cdm_physicalcu remains constant and rest is dynamic .The address for this structure must be the same even if the "rest values" changes .Vector do use a dynamically allocated array to store their elements. But how about malloc.Since I have a linked structure eg: a -> b->c and i need values of c to be present in "b" and the values of "b" in "a"
@TechBrkTru: "how about malloc" - like I said, don't use C idioms in C++. You could cobble something together with malloc, taking great care to avoid memory leaks and worse, but vector does essentially the same thing while managing all the memory allocations for you.
:What if there would another structure named for example : typdef struct data { int a ; char b result res;} . Can we get the values of structure abc within structure data through vector??
@TechBrkTru: Yes, you can access class members in the usual manner. Given data d; you can access d.res.details[2].name.
|
-1

I think if you want to use malloc, you should define a pointer that points to the struct abc. Then if you find a new one you can malloc new memory and access it via the pointer. I also think the STL vector will be a better choice. It is very convenient. You can learn some knowledge about STL. That's so interesting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.