char * is fail in CArray while char[] is success

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Experts,
i have following structure

typedef struct xy{
int num;
char *val;
}XY;

struct XYArray:public CArray<XY,XY>
{
};

in cpp file, in a function
struct XYArray Obj;
Now i can add XY type variable to Obj like
XY A;
A.num=1; A.val=str;
// suppose str is a char *array which contains some value.
Obj.Add(A); // add XY type variable to Obj
Suppose i add 10 elements to Obj. Now when i get each element of Obj then i
found that value of val field of XY structure has been change. i.e.
XY B=Obj.GetAt(0);
B.val is not which i add in Obj.

While if i declare XY as
typedef struct xy{
int num;
char val[250];
}XY;
and add data to Obj like
XY A;
A.num=1;
memcpy(A.val,str,strlen(str)); // suppose stris a char *array which contains
some value.
Obj.Add(A);

In this case when i get each element of Obj, i found right value.


So please tell how to solve this problem. why char * is fail in CArray?
while char[] is success.


.



Relevant Pages

  • Re: char * is fail in CArray while char[] is success
    ... struct XYArray:public CArray ... Now i can add XY type variable to Obj like ... // suppose str is a char *array which contains some value. ... CArray<int, int> numbers; ...
    (microsoft.public.vc.mfc)
  • [v2] Generic LRU cache built on a kmem_cache and a "struct shrinker"
    ... register a shrinker callback (needs a small patch to pass a pointer to ... the "struct shrinker" to the callback) which allows us to respond to ... When an object needs to be moved between the in_use list and the LRU, ... If you return nonero from this function, "obj" will be immediately ...
    (Linux-Kernel)
  • Re: polymorphic structs with "methods"
    ... typedef struct _Object { ... Object obj; ... void prettyPrintNumber{ ... void prettyPrintNumber(Number *num) { ...
    (comp.lang.c)
  • Re: size of a struct without "sizeof"
    ... Richard Heathfield writes: ... Just sizeof. ... struct whatever array; ... &obj is the address of obj, ...
    (comp.lang.c)
  • polymorphic structs with "methods"
    ... switch statement to call the appropriate function for that type. ... typedef struct _Object { ... Object obj; ... void prettyPrintNumber{ ...
    (comp.lang.c)