Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

whats the differance, copying strings

This topic is 3920 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

im having some issues with copying strings... im trying to copy a string from a temporary adress to a permanent using the string copy function (CASE2)... but it doesnt work... instead i have to do it like in CASE1, which for some reason works... im sending the string as a shader variable later on... and in CASE1 i get correct results whereas CASE2 is wrong... i dont rly see whats the difference?
// CASE 1
int size = 12;
temp.Name = new char[size+1];
for (int k = 0; k < size; k++)
{
	temp.Name[k] = d3dxEffects.pDefaults[n].pParamName[k];
}
temp.Name[size] = '\0';


//CASE 2
StringCchCopyA(temp.Name, MAX_PATH, d3dxEffects.pDefaults[n].pParamName);  


Share this post


Link to post
Share on other sites
Advertisement
Do you allocate temp.Name in CASE 2?
int size = 12+1;
temp.Name = new char[size];
StringCchCopyA(temp.Name, size, d3dxEffects.pDefaults[n].pParamName);


But better yet...



struct Whatever {
...
std::string Name;
};

...

Whatever temp;

...

temp.name = d3dxEffects.pDefaults[n].pParamName;

...

// to pass to shader:
foo( temp.name.c_str() );

Share this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
thx that solved it.. had to use the data() function to get it working


Be aware that std::string::data() does not necessarily return a null-terminated string, whereas std::string::c_str() does.

Antheus's second example above shows the usage of c_str(). When you want a null terminated C-style string, you should prefer this over data().

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!