Thread: sending in char pointer, using string

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    7

    sending in char pointer, using string

    Hi,

    If anyone can help it would be greatly appreciated. I have a function which creates a StrBuf called _buf. Now I want to send this _buf into another member function which only accepts char *pointer ie. character pointer. Here is the member function:

    Code:
      
    
    void Token::setStr(const char* str)
    {_str = (char*)str;}
    Note: _str is a private variable.


    What must I do to _buf so that I can send it as a parameter for the setStr function.

    Can I do something like:

    char *temp[] = _buf;
    char *temppointer = temp;
    then send in tempointer to the setStr funtion? ie. Object.setStr(&temppointer)

    Thanks if anyone can offer some guidance and assistance.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have a function which creates a StrBuf called _buf.
    Is StrBuf a class you've created? Or are you talking about a wierd typedef of the standard stringbuf? If it's the former then just create a conversion to const char * through either an overloaded implicit conversion (not usually a good idea) or an explicit member function (preferred).

    >_str = (char*)str;
    If you want to cast away const, use the C++ casts so that readers know exactly what you're doing:

    _str = const_cast<char *> ( str );

    >What must I do to _buf so that I can send it as a parameter for the setStr function.
    We need to know a little more about _buf to guide you properly, I don't know of any standard type called StrBuf, so I have to assume that you've written it.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM