casting from void* to const char *

L

Larry

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
 
E

Eric Pruneau

Larry said:
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?

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

Eric Pruneau
 
L

Larry

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
 
R

Richard Herring

Larry said:
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);

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
anyway said:
}
};

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top