Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > casting from void* to const char *

Reply
Thread Tools

casting from void* to const char *

 
 
Larry
Guest
Posts: n/a
 
      01-25-2010
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

 
Reply With Quote
 
 
 
 
Eric Pruneau
Guest
Posts: n/a
 
      01-26-2010

"Larry" <(E-Mail Removed)> a écrit dans le message de news:
4b5e2ccd$0$1144$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Larry
Guest
Posts: n/a
 
      01-26-2010

"Larry" <(E-Mail Removed)> ha scritto nel messaggio
news:4b5e2ccd$0$1144$(E-Mail Removed). ..

> 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

 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      01-26-2010
In message <4b5e56aa$0$1132$(E-Mail Removed)>, Larry
<(E-Mail Removed)> writes
>
>"Larry" <(E-Mail Removed)> ha scritto nel messaggio
>news:4b5e2ccd$0$1144$(E-Mail Removed) ...
>
>> 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
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Is char** (or char*[]) implicitly convertible to 'const char * const *'? kevin.hall@motioneng.com C Programming 24 10-30-2005 08:07 AM



Advertisments