How to convert const char *' to 'CHAR [32]'

This is a discussion on How to convert const char *' to 'CHAR [32]' within the C++ Programming forums, part of the General Programming Boards category; I have to assing and std::string to a D3DXFONT_DESC font value and I can't figure the conversion. Code: D3DXFONT_DESC lf; ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    How to convert const char *' to 'CHAR [32]'

    I have to assing and std::string to a D3DXFONT_DESC font value and I can't figure the conversion.
    Code:
            D3DXFONT_DESC lf;
            std::string name = "blah";
            lf.FaceName = name.c_str();
    This produces

    error C2440: '=' : cannot convert from 'const char *' to 'CHAR [32]'

    I have boost::lexical_cast avaiable to me but I am not sure what the type is. Or is there a simpler way to do this.

    Regards

    Chad

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably don't WANT to do that, what you do want to do is assign an array lt.facename with the content of the string.c_str(). If that is what you want to do, then you should use strcpy() to copy the c_str() to facename.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by matsp View Post
    You probably don't WANT to do that, what you do want to do is assign an array lt.facename with the content of the string.c_str(). If that is what you want to do, then you should use strcpy() to copy the c_str() to facename.

    --
    Mats
    Hmm not sure if I am grasping what you are suggesting.

    Code:
    	char str1[] = strcpy(str1, name.c_str());
    Obviously this peice of code does not compile. Any suggestions?

    Thanks

    Chad
    Last edited by chadsxe; 02-16-2009 at 09:45 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to define how many characters are in the str1 array. And you cannot assign to an array (you can initialize it, yes, but strcpy is not initialization).

  5. #5
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Try this
    Code:
    strcpy(lf.FaceName, name.c_str());
    Or if you want to be optimal about it
    Code:
    strncpy(lf.FaceName, name.c_str(), name.length() + 1);
    And if you're like me you'll put
    Code:
    assert(name.length() < 32);
    before the str*cpy()

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chadsxe View Post
    Hmm not sure if I am grasping what you are suggesting.

    Code:
    	char str1[] = strcpy(str1, name.c_str());
    Obviously this peice of code does not compile. Any suggestions?

    Thanks

    Chad
    strcpy is a function that copies the second string argument into the first one - it does return the string you have as input, but the syntax of the line you describe isn't meaningull.

    Note that in C (and C++) you can not assign a values to an array (other than as a constant initializer) using = - you have to assign each element as a single item - or call a function that does it for you, in this case.

    I'm 99% sure that what you want in this case is
    Code:
    strcpy(lf.FaceName, name.c_str());
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    To all poster.

    Thanks for the responses.

    Both the strcpy and strncpy method work, but is it not possible to use the _s version of these functions?

    Also, what is the difference between strcpy and strcpy. Is it just a length parameter?

    Regards

    Cjad
    Last edited by chadsxe; 02-16-2009 at 10:05 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Correct me if I am wrong but this is how you would use the _s versions correct?

    strncpy_s(lf.FaceName, 32, name.c_str(), name.length() + 1);
    strcpy_s(lf.FaceName, 32, name.c_str());

    Now to figure out which one to use.

    Regards

    Chad

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Quote Originally Posted by chadsxe
    Also, what is the difference between strcpy and strcpy. Is it just a length parameter?
    I think you meant "strcpy and strncpy", in which case the answer is yes. When it comes to avoiding buffer overflow, strncpy is generally safer than strcpy.

    Quote Originally Posted by chadsxe
    Now to figure out which one to use.
    For better or for worse, the _s versions are non-standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by laserlight View Post
    I think you meant "strcpy and strncpy", in which case the answer is yes. When it comes to avoiding buffer overflow, strncpy is generally safer than strcpy.


    For better or for worse, the _s versions are non-standard.
    Yeah I realize this now. There Microsoft platform depenedent. For educational sakes am I correct in my assumption that since lf.FaceName is a CHAR[32] then my destination size parameter of the _s functions would have to be 32?

    Regards

    Chad

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Yes, but sizeof(lf.FaceName) would work better.
    Though the *_s versions are non-standard, they do work better in finding bugs and making your application a tad safer. Plus they are generally easy to write wrappers around.
    So it's up to you if you want to use them.
    And as for why the first example didn't work... the API function has a buffer that it wants you to fill in with the name of the font. It does not want a pointer to the string (it's an array).
    Also, you should never really cast away const, unless absolutely necessary (very few valid reasons there).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. Playing card data structure?
    By crypticgeek in forum C++ Programming
    Replies: 10
    Last Post: 12-31-2006, 04:29 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM

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