Sign in to follow this  
squicklid

converting const char[n] to const char*

Recommended Posts

squicklid    132
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?

Share this post


Link to post
Share on other sites
Boku San    428
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.

Share this post


Link to post
Share on other sites
squicklid    132
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.

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6921
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
i believe the OP said he did that and got a conversion error.
well unless i missed something

Yeah, you missed the post where he said:
Quote:
Original post by squicklid
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.


Even though that conversion is invalid :p I think he meant

const char s1[] = "mantis";
const char * s2 = s1;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Many people think an array is just a pointer. It isn't. It's easy to demonstrate.

[ccode]
char[5] foo = {'a', 'b', 'c', 'd', '\0'};
char *bar = "abcd";
[/ccode]

As everyone knows, they can be printed like this:

[ccode]
printf(foo);
printf(bar);
[/ccode]

But what happens if you do:

[ccode]
printf(&foo);
printf(&bar);
[/ccode]

The first one actually prints the text, while the second one will print nothing or garbage, maybe even segfault.

Just thought I'd mention it now that we're discussing arrays and pointers.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Now, to go completely offtopic... How about putting a legend with the common tags on the reply page. It's annoying how every forum has their own tags.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this