Sign in to follow this  
OOBrad21

realllly basic char* question

Recommended Posts

My problem is really elementary and yet I've been perplexed by hours by it.. I have a class called "rt_Material". I have this private class var: private: char * texture_name; In the constructor, i pass a char*, and assign it to texture_name rt_Material::rt_Material( ..., char *n,...) cout<<n<<endl; texture_name = n; cout<<texture_name<<endl; { Then, i have an accessor for it: char* rt_Material::getTextureName(){ cout<<"Texture name: "<<*texture_name<<endl; return rt_Material::texture_name; } Now, notice my couts? the first 2 in the constructor always have the correct value. However, the one in the accessor prints out garbage, or nothing at all. There has to be something easy im missing. any ideas?

Share this post


Link to post
Share on other sites
Are you passing a string literal? If you are, then memory hasn't really been allocated and is probably being invalidated when you go out of scope, in which case you'll have to copy the data from your pointer into a member variable.

I would suggest you forget about using char and start using std::string.

Share this post


Link to post
Share on other sites
Quote:
Original post by CrimsonSun
Are you passing a string literal? If you are, then memory hasn't really been allocated and is probably being invalidated when you go out of scope.

I would suggest you forget about using char and start using std::string.


Ah, yes, this makes sense. I had played around with using std::string, but the accessor requires that i return a char*, and when i do string.c_str() it gives me a const char*. Im sure there is a quick way around this?

Share this post


Link to post
Share on other sites
Yes, you should definitely be using std::string instead of C-style strings. Code in C++, not FrankenC. [grin]

That aside, though, I see two problems with this code:
char* rt_Material::getTextureName(){
cout<<"Texture name: "<<*texture_name<<endl;
return rt_Material::texture_name;
}
First, you're dereferencing your texture_name pointer. If your intention is to print the string, you should not do this. Note that your cout statements in the constructor do not do this.

Second, you're returning rt_Material::texture_name, as though it were a static variable, when it pretty clearly is not. This should not even compile; was this just a transcription error, perhaps?

Share this post


Link to post
Share on other sites
Quote:
Original post by MrAccident
Yes, you should definitely be using std::string instead of C-style strings. Code in C++, not FrankenC. [grin]

That aside, though, I see two problems with this code:
char* rt_Material::getTextureName(){
cout<<"Texture name: "<<*texture_name<<endl;
return rt_Material::texture_name;
}
First, you're dereferencing your texture_name pointer. If your intention is to print the string, you should not do this. Note that your cout statements in the constructor do not do this.

Second, you're returning rt_Material::texture_name, as though it were a static variable, when it pretty clearly is not. This should not even compile; was this just a transcription error, perhaps?



Both of the things you pointed out were just just acts of desperation by me. :)
I think crimsonsun hit the nail on the head, Now all thats left is to figure out how to get a char* out of a std::string

Share this post


Link to post
Share on other sites
Quote:
Original post by OOBrad21
Quote:
Original post by MrAccident
Yes, you should definitely be using std::string instead of C-style strings. Code in C++, not FrankenC. [grin]

That aside, though, I see two problems with this code:
char* rt_Material::getTextureName(){
cout<<"Texture name: "<<*texture_name<<endl;
return rt_Material::texture_name;
}
First, you're dereferencing your texture_name pointer. If your intention is to print the string, you should not do this. Note that your cout statements in the constructor do not do this.

Second, you're returning rt_Material::texture_name, as though it were a static variable, when it pretty clearly is not. This should not even compile; was this just a transcription error, perhaps?



Both of the things you pointed out were just just acts of desperation by me. :)
I think crimsonsun hit the nail on the head, Now all thats left is to figure out how to get a char* out of a std::string


Why do you need to return a pointer to a char? If you need to modify the variable, return a reference or a pointer to a string - modifying a string is a million flavors more fun than modifying a character array.

Share this post


Link to post
Share on other sites
Quote:
Original post by CrimsonSun
Quote:
Original post by OOBrad21
Quote:
Original post by MrAccident
Yes, you should definitely be using std::string instead of C-style strings. Code in C++, not FrankenC. [grin]

That aside, though, I see two problems with this code:
char* rt_Material::getTextureName(){
cout<<"Texture name: "<<*texture_name<<endl;
return rt_Material::texture_name;
}
First, you're dereferencing your texture_name pointer. If your intention is to print the string, you should not do this. Note that your cout statements in the constructor do not do this.

Second, you're returning rt_Material::texture_name, as though it were a static variable, when it pretty clearly is not. This should not even compile; was this just a transcription error, perhaps?



Both of the things you pointed out were just just acts of desperation by me. :)
I think crimsonsun hit the nail on the head, Now all thats left is to figure out how to get a char* out of a std::string


Why do you need to return a pointer to a char? If you need to modify the variable, return a reference or a pointer to a string - modifying a string is a million flavors more fun than modifying a character array.


Im using a precompiled function in a .o file that only accepts char*. so I somehow need to get a string to a non-const char*....

Share this post


Link to post
Share on other sites
Quote:
Original post by CrimsonSun
If you really need to return a non-const char pointer (which you really shouldn't), you can const_cast<char*> your string's c_str() return.


Yeah, i don't have a choice since im working with an object file. const_cast worked, thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by CrimsonSun
If you really need to return a non-const char pointer (which you really shouldn't), you can const_cast<char*> your string's c_str() return.


The OP seems to need a non-const char* because of some library function that requests as much. In this kind of situation, there's nothing wrong with const_cast, it's probably the best solution as you've already pointed out. Stuff like this is one of the reasons const_cast exists in the first place.

Share this post


Link to post
Share on other sites
Quote:
Original post by OOBrad21
Im using a precompiled function in a .o file that only accepts char*. so I somehow need to get a string to a non-const char*....


What function/object file is it? Perhaps I (we) can help you replace it with something saner (i.e. more modern).

Anyway. The thing that you have to understand about pointers is that they're, well, just pointers. You have to be able to reason about what they point to, at all times. (Actually, a string literal *would* be fine, but, say, contents of a local char[] buffer in the calling function would *not*.)

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