Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

casting from void* to const char *

0 views
Skip to first unread message

Larry

unread,
Jan 25, 2010, 6:44:13 PM1/25/10
to
Hi!

I have the following struct:

struct cBuffer
{
char data[1024];
int bytesRecorded;
bool flag;
cBuffer(char * data_, int bytesRecorded_, bool flag_) :
bytesRecorded(bytesRecorded_), flag(flag_)
{
memcpy(static_cast<void *>(data), static_cast<void *>(data_),
bytesRecorded);
}
};

I would like to turn char data[1024] to const char data[1024], the same goes
for the constructor:

cBuffer(const char * data_, int bytesRecorded_, bool flag_)...

But then I fail using the memcpy() function because of the wrong cast.

how could I sort that out?

thanks

Eric Pruneau

unread,
Jan 25, 2010, 7:48:07 PM1/25/10
to

"Larry" <dontme...@got.it> a �crit dans le message de news:
4b5e2ccd$0$1144$4faf...@reader1.news.tin.it...

Well your example compile and run fine... what is failling exactly?

Eric Pruneau


Yu Han

unread,
Jan 25, 2010, 8:57:06 PM1/25/10
to
On 01/26/2010 07:44 AM, Larry wrote:
> Hi!
>
> I have the following struct:
>
> struct cBuffer
> {
> char data[1024];
> int bytesRecorded;
> bool flag;
> cBuffer(char * data_, int bytesRecorded_, bool flag_) :
> bytesRecorded(bytesRecorded_), flag(flag_)
> {
> memcpy(static_cast<void *>(data), static_cast<void *>(data_),
No cast needed...

> bytesRecorded);
> }
> };
>
> I would like to turn char data[1024] to const char data[1024], the same
> goes for the constructor:
>
> cBuffer(const char * data_, int bytesRecorded_, bool flag_)...
>
> But then I fail using the memcpy() function because of the wrong cast.
>
> how could I sort that out?
>
> thanks
>


--
Yu Han

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Yu Han

unread,
Jan 25, 2010, 8:59:51 PM1/25/10
to
On 01/26/2010 07:44 AM, Larry wrote:
> Hi!
>
> I have the following struct:
>
> struct cBuffer
> {
> char data[1024];
> int bytesRecorded;
> bool flag;
> cBuffer(char * data_, int bytesRecorded_, bool flag_) :
> bytesRecorded(bytesRecorded_), flag(flag_)
> {
> memcpy(static_cast<void *>(data), static_cast<void *>(data_),
See my last post; static_cast<void const*>

> bytesRecorded);
> }
> };
>
> I would like to turn char data[1024] to const char data[1024], the same
> goes for the constructor:
>
> cBuffer(const char * data_, int bytesRecorded_, bool flag_)...
>
> But then I fail using the memcpy() function because of the wrong cast.
static_cast could not cast const to non-const

>
> how could I sort that out?
>
> thanks
>

Larry

unread,
Jan 25, 2010, 9:42:50 PM1/25/10
to

"Larry" <dontme...@got.it> ha scritto nel messaggio
news:4b5e2ccd$0$1144$4faf...@reader1.news.tin.it...

> I have the following struct:

no problem anymore. I have changed to this:

#include <algorithm>

struct buffer


{
char data[1024];
int bytesRecorded;
bool flag;

buffer(const char * data_, const int bytesRecorded_, const bool flag_) :
bytesRecorded(bytesRecorded_), flag(flag_)
{
std::copy(data_, data_ + (bytesRecorded_ * sizeof(char)), data);
}
};

thanks

Richard Herring

unread,
Jan 26, 2010, 5:26:07 AM1/26/10
to
In message <4b5e56aa$0$1132$4faf...@reader3.news.tin.it>, Larry
<dontme...@got.it> writes

That's wrong.

(OK, it doesn't actually matter here, because sizeof(char) is 1 by
definition, but your code is evidence of a number of misconceptions
which will bite you later:)

If bytesRecorded really is a count of bytes, you shouldn't be
multiplying it by sizeof(anything), because it's already a measure of
size.

If bytesRecorded is actually a badly-named count of objects, you still
shouldn't be multiplying it by sizeof(anything) because std::copy copies
objects, not bytes, and its arguments are iterators to objects, not
pointers to memory.

And you should be replacing data and bytesRecorded by a
std::vector<char>, anyway, and using its initializer to insert the
data..

>}
>};
>
>thanks

PS Most people who use the trailing-underscore convention do it the
other way round: member variables get the suffix, local variables and
parameters don't.
--
Richard Herring

0 new messages