convert char* to const char*

This is a discussion on convert char* to const char* within the C Programming forums, part of the General Programming Boards category; Hi, how do i convert char* to const char* in the following example? Code: UCHAR ucar[3]; ucar[0]='a'; ucar[1]='b'; ucar[2]='c'; //convert ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    19

    convert char* to const char*

    Hi,
    how do i convert char* to const char* in the following example?
    Code:
    	UCHAR ucar[3];
    	ucar[0]='a';
    	ucar[1]='b';
    	ucar[2]='c';
    	
                //convert char* to  const char*
                //const char* car;
    
    	printf(car);

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    -------------------------Method 1------------------------
    Declare your array as CHAR (recommended)

    -------------------------Method 2-------------------------
    Cast to (char*) ---> printf((char*)ucar);
    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.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You don't need to cast something from type to const type because if you pass type to a function expecting a const type, it will automatically be upgraded.

    If you have a const type and need to pass it to a function that isn't const-correct, you can cast it like Elysia suggested.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Unfortunately, this is unsigned char* and printf expects char*, thus causing a compile error in C++ and a warning in C.
    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.

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Quote Originally Posted by Elysia View Post
    Unfortunately, this is unsigned char* and printf expects char*, thus causing a compile error in C++ and a warning in C.
    Shouldn't matter since OP isn't dealing with them arithmetically and neither does printf() with the format string. I am not sure what OP is trying to accomplish other than buffer overrun though.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Shouldn't matter, but does matter since it invokes a warning. Thus, a cast or change of types would be the best solution - compilers output warnings for a reason and it's best to satisfy the compiler by solving them.
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    The lack of a \0 in the string is another problem to be addressed.
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    I just saw that now. That would be why we usually do
    Code:
    UCHAR ucar[] = "abc";
    Or
    Code:
    UCHAR uchar[4];
    strcpy((char*)uchar, "abc");
    Though I'm not suer if "abc" will be considered just const char* and require a cast to unsigned const char*.
    Usually, unsigned char is typically used for buffers; when you use strings, normal char is what you use.
    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. The so annoying LNK2005... please help!
    By Mikey_S in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2009, 04:22 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  4. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 05:32 PM
  5. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM

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