converting const char[n] to const char*
Apparently, you cannot do this:
const char str[] = "mantis";
void f(const char * paramstr);
and then call that function like:
f(str);
because const char[6] is different from const char *.
So, is it possible to do this another way?
Wow. I was not aware of that.
You can still change the first part to a
const char str* = "heelo";
You can still change the first part to a
const char str* = "heelo";
Wait...I feel like I know this, but I don't know how to word this correctly. So, expect stupidity to flow like a river.
Anyway, try declaring char str[] = "mantis"; and then void f(const char* paramstr).
Why do you say you "can't do it"? It looks like it should work alright anyway.
Anyway, try declaring char str[] = "mantis"; and then void f(const char* paramstr).
Why do you say you "can't do it"? It looks like it should work alright anyway.
Nevermind this was a waste of a thread.
I was just a loser. A LOSER!
It works.
You just can't initialize a variable like:
const char s1[] = "mantis";
char * s2 = s1;
And I got carried away.
I was just a loser. A LOSER!
It works.
You just can't initialize a variable like:
const char s1[] = "mantis";
char * s2 = s1;
And I got carried away.
if i remember correctly....,
a char array[n] actually means char * const array
so const char array [n] is actually const char * const array.
remember a char* array can point to any memory location
a char array[] is contigious allotment that will not change memory locations just values placed in memory
a char array[n] actually means char * const array
so const char array [n] is actually const char * const array.
remember a char* array can point to any memory location
a char array[] is contigious allotment that will not change memory locations just values placed in memory
Quote:Original post by Alpha_ProgDes
if i remember correctly....,
a char array[n] actually means char * const array
so const char array [n] is actually const char * const array.
remember a char* array can point to any memory location
a char array[] is contigious allotment that will not change memory locations just values placed in memory
No, they are two different things. const char array[n] makes an array of contiguous memory. Using the array name refers to the entire array without any indirection without any pointers involved. A const char* on the other hand does not directly represent an array. It is just a variable that points to a char (which may or may not even be a part of an array). There is one level of indirection. The are different in both implementation and concept.
Err???? Can't you typecast it????? I think you can......
f( (const char*)str );
or
f( (const char*)&str[0] );
f( (const char*)str );
or
f( (const char*)&str[0] );
Quote:Original post by Pipo DeClown
Err???? Can't you typecast it????? I think you can......
f( (const char*)str );
or
f( (const char*)&str[0] );
Yup. And actually, you don't even have to explicitly do that. It's implicit. You really can just do f( str );
i believe the OP said he did that and got a conversion error.
well unless i missed something
well unless i missed something
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement