Thread: casting

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    casting

    I want to send a a (void *) to a function. (And the function sends the data to te rs232 port.)

    If I have a char I can just use
    char MyMessage[3] = "hei";
    void *Message = MyMessage;

    myfunction(Message);

    How can I use unsigned int, Byte and unsigned char??

    I have tried to cast them to (void *) but without success...

    I also wonder how I can use strcat with differnts types..
    BYTE number;
    strcat(ex1, number) dosn't seems to work..


    Please help me!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Given a function like this
    void myFunction ( const void *data, size_t len );

    Then say
    char message[]="hello";
    myFunction( message, strlen(message) );

    int foo = 123;
    myFunction( &foo, sizeof(foo) );


    char ch = '@';
    myFunction( &ch, sizeof(ch) );

    etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by EirikO View Post
    If I have a char I can just use
    char MyMessage[3] = "hei";
    void *Message = MyMessage;
    no you cant, because "hei" includes the '\0' character so you need 4 chars for MyMessage

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > no you cant, because "hei" includes the '\0' character so you need 4 chars for MyMessage
    It's actually valid (if somewhat dubious) to initialise an 'n' char array with 'n' characters in C.
    It's not valid in C++ though.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I also wonder how I can use strcat with differnts types..
    >BYTE number;
    >strcat(ex1, number) dosn't seems to work..
    Try sprintf().
    Code:
    BYTE number = 123;
    char buf[100];
    
    sprintf(buf, "%s %d", ex1, (int) number);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2007, 11:06 PM
  2. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM