casting

This is a discussion on casting within the C Programming forums, part of the General Programming Boards category; I want to send a a (void *) to a function. (And the function sends the data to te rs232 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21