casting from void* to const char *

Discussion in 'C++' started by Larry, Jan 25, 2010.

  1. Larry

    Larry Guest

    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
     
    Larry, Jan 25, 2010
    #1
    1. Advertising

  2. Larry

    Eric Pruneau Guest

    "Larry" <> a écrit dans le message de news:
    4b5e2ccd$0$1144$...
    > 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
     
    Eric Pruneau, Jan 26, 2010
    #2
    1. Advertising

  3. Larry

    Larry Guest

    "Larry" <> ha scritto nel messaggio
    news:4b5e2ccd$0$1144$...

    > 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
     
    Larry, Jan 26, 2010
    #3
  4. In message <4b5e56aa$0$1132$>, Larry
    <> writes
    >
    >"Larry" <> ha scritto nel messaggio
    >news:4b5e2ccd$0$1144$...
    >
    >> 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
     
    Richard Herring, Jan 26, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Enrico `Trippo' Porreca

    Casting const void * into void *

    Enrico `Trippo' Porreca, May 31, 2004, in forum: C Programming
    Replies:
    14
    Views:
    991
    August Derleth
    Jun 4, 2004
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,152
    Ian Collins
    May 9, 2006
  3. Replies:
    11
    Views:
    1,152
  4. Alex Vinokur
    Replies:
    9
    Views:
    833
    James Kanze
    Oct 13, 2008
  5. 0m
    Replies:
    26
    Views:
    1,174
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page