const void* problem

This is a discussion on const void* problem within the C++ Programming forums, part of the General Programming Boards category; Hi there! I'd like to use a Comm Port object. It has a Write function, which has two variable inputs. ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    105

    const void* problem

    Hi there!

    I'd like to use a Comm Port object. It has a Write function, which has two variable inputs. One is a const void*, the other is an int.

    The const void* is the variable sent out via a comport, and the int is the length of the characters to send.

    My question is: How can I make a const void* from a simple AnsiString?

    Thanks!
    Han

  2. #2
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Should be a simple matter of casting. Something like this:

    Code:
    char myString[200];
    string myString2;
    // do stuff
    const void* constVoidString = (const void*)myString;
    const void* constVoidString2 = (const void*)myString2.c_str();

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You don't have to cast a pointer when assigning to a void*, only when you are going from a void* to a typed pointer

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    Thanks!

    I forgot about the brackets It's been awhile since I did things like this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 05:32 PM
  5. problem with the const variable
    By ssharish in forum C Programming
    Replies: 2
    Last Post: 01-28-2005, 09:53 AM

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