Jump to content
  • Advertisement
Sign in to follow this  
squicklid

converting const char[n] to const char*

This topic is 5040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!