Hello

I need help in converting char * to TByteDynArray in Borland C++ Builder.

Below code is suppose to work but it give me EAccess Voilation error.

char *one = "test";
TByteDynArray two;
two.set_length(sizeof(one));
Move((void *) one[0], (void *) two[0],sizeof(one));

This kind of code work in delphi and it is also suppose to work in c+ builder but not working for me.

Thanks for any help anyone can provide.

Best Regards

Alen

> two.set_length(sizeof(one));
sizeof tells you the size of the pointer.
strlen() tells you the length of the string that the pointer points to.

> Move((void *) one[0], (void *) two[0],sizeof(one));
I imagine your random casting has made the compiler shut up about important errors in this line.
1. From the look of things, TByteDynArray is a class with member functions, and it probably doesn't take too kindly to being written at using a brute force memory move. Does the class overload the [ ] operator by chance? If so, use a loop to move each character in turn.

2. If it's anything like the standard memmove() or memcpy() functions, then the parameter order is destination, source, length. In other words, you're copying TO one, not from it. This is an immediate fault as many implementations make string constants read-only.
In fact, it's even worse than that, it's taken the first letter of the string, cast that into a pointer, then tried to scribble all over it.

3. The afore-mentioned sizeof is not strlen problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.