char^ to const char* casting 
Author Message
 char^ to const char* casting

What I have looks like this:

void function(char* variable)
{
  const char* var = variable;
  function_that_needs_const_char(var);
  return;

Quote:
}

Is this:
a.) correct?
b.) the best way to do this?

Thanks for help!

rt



Thu, 02 Aug 2001 03:00:00 GMT  
 char^ to const char* casting

Quote:
>What I have looks like this:

>void function(char* variable)
>{
>  const char* var = variable;
>  function_that_needs_const_char(var);
>  return;
>}

>Is this:
>a.) correct?
>b.) the best way to do this?

a.) yes
b.) no.  YOu don't need var; you can just code

        function_that_needs_const_char(variable);

--
Michael M Rubenstein



Thu, 02 Aug 2001 03:00:00 GMT  
 char^ to const char* casting


Quote:
>What I have looks like this:

>void function(char* variable)
>{
>  const char* var = variable;
>  function_that_needs_const_char(var);
>  return;
>}

>Is this:
>a.) correct?

Yes, conversion from a pointer to a type to a to a pointer to a
more qualified version of that type is "safe" and supported in C
as an explicit conversion. Essentially a pointer to a const type
indicates that you won't modify what the pointer points to using that
pointer or a pointer derived from it.

Quote:
>b.) the best way to do this?

Since it is supported implicitly there is no need to use an extra
varaible just for the conversion:

void function(char* variable)
{
  function_that_needs_const_char(variable);
  return;

Quote:
}

You probably use this sort of thing all of the time without thinking
about it e.g.

   printf("Some text\n");

A string literal has type array of char which gets converted to a
pointer to its first element (i.e. a char *) in a value context like this.
printf() requires a const char * as its first argument and the compiler
performs the conversion automatically. Other standard library functions
like strcmp(), fopen() also take const char * arguments and are happy
to take char *.

--
-----------------------------------------


-----------------------------------------



Thu, 02 Aug 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help: inline char const* const& max(char const* const &a, char const* const &b)

2. const char * vs char const *

3. const char *p == char const *p ?

4. char** and const char** ...

5. va_arg(ap, const char *) = char*?

6. const char* to char[64]

7. const char ** incompatible with char ** (and vice versa)?

8. passing const char* to char* fails

9. const char* vs. char *

10. converting const char* to unsigned char[1024]

11. char *'s and const char *'s

12. const char* to char* ?

 

 
Powered by phpBB® Forum Software