DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Posts
    242

    RWCString to char

    Hello,

    Can someone please tell me whether there is a way to cast RWCString to char* ?

    I have :-

    RWCString name = "...";
    char nnnnnnn[100] = "";

    and I want to copy the contents of name to nnnnnnn.

    I know I can do :-

    char m_nnnnnnn[1000] = "";
    strcpy(m_nnnnnnn, name);

    but, I was hoping to do something nicer than that.

    Any ideas please ?

    Many thanks.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    an array of char requires moving the items into it using the limited ways that arrays support:

    a loop
    one by one copy: a[0] = b[0], a[1] = b[1], ... etc
    a copy (strcpy, memcpy, etc)
    or the like, all of which are crude.

    If you want something better, you need an object, even if its just a wrapper around a char array to give it an assignment operator.

    The cleanest way then is maybe

    char* cp;
    string str;
    str = your_RWCString;
    cp = & str[0];
    ... use cp as if it were a C-string if that is needed for some reason, such as a byte-array packet for network transmission?

    Or, if you do not actually need the char array at all, just use the string?

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    IF you really have to use a char array, there's no escape from calling a function that will copy the buffer from name. Look at the std::copy algorithm or plain old strncpy().
    Danny Kalev

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Just use
    Code:
    RWCString rws = "Copy me";
    // 1000 should be big enough, but of course you can also dynamically allocate and delete later
    char buffer[1000] = {0};
    
    strcpy(buffer,rws.data());
    But you might not need to copy to a buffer at all. It depends on what you want to do with it.
    if you only need a const char* (for example as the input of an API call) then you shouldn't even
    bother copying, but just use the data() method of RWCString.
    Last edited by drkybelk; 04-16-2010 at 05:19 PM.
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

Similar Threads

  1. Runtime error w/ Variable Type Part II
    By jamestmfbong in forum C++
    Replies: 2
    Last Post: 05-04-2009, 06:03 PM
  2. Gradebook program
    By [gx]Shadow in forum Java
    Replies: 5
    Last Post: 10-25-2006, 10:20 PM
  3. Replies: 9
    Last Post: 07-28-2005, 08:40 PM
  4. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 2
    Last Post: 09-01-2000, 11:36 AM
  5. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 0
    Last Post: 09-01-2000, 08:59 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links