C String Manipulation
I'm looking for some major help in manipulating strings in C. I'm basically trying to remove the first character from the string and concatenate it with another. However, the string I'm trying to manipulate is in the form of a pointer, and I can't figure out how to convert it to a string.
char *strg = m_pMaterials[x].m_pTextureFilename;
Is the line I'm having problems with. However, I have no clue to (if I have to) convert it to a normal string and manipulate it.
Any help is greatly appreciated!!!
[edited by - clueless_programmer on January 24, 2003 1:26:11 AM]
January 24, 2003 12:42 AM
use strcat()
strcat( firstString, ++secondString );
making sure that firstString is large enough to hold the fully concatenated string.
The ++ in front of secondString means it will see the memory address of the string starting at the second character in the string.
ie. if secondString points to "Hello World" then ++secondString will point to "ello World"
Hope that helps
strcat( firstString, ++secondString );
making sure that firstString is large enough to hold the fully concatenated string.
The ++ in front of secondString means it will see the memory address of the string starting at the second character in the string.
ie. if secondString points to "Hello World" then ++secondString will point to "ello World"
Hope that helps
strcat doesn't care if you have enough space in the firststring, since it returns a pointer to the new string anyway (it allocates more memory, if needed).
try this:
<textarea>
</textarea>
Edit: WTF is wrong with this forum software, it kills my code!
Height Map Editor | Eternal lands
[edited by - Raduprv on January 24, 2003 7:33:29 PM]
try this:
<textarea>
char concatenated_string[a large enough number];for(i=1;i;for(j=0;jconcatenated_string[i-1+j]=0;
</textarea>
Edit: WTF is wrong with this forum software, it kills my code!
Height Map Editor | Eternal lands
[edited by - Raduprv on January 24, 2003 7:33:29 PM]
quote: Original post by Raduprv
strcat doesn''t care if you have enough space in the firststring, since it returns a pointer to the new string anyway (it allocates more memory, if needed).
From MSDN: (bold and underlining mine)
quote:
The strcat function appends strSource to strDestination and terminates the resulting string with a null character. The initial character of strSource overwrites the terminating null character of strDestination. No overflow checking is performed when strings are copied or appended. The behavior of strcat is undefined if the source and destination strings overlap.
Spreading misinformation is more dangerous than admitting you don''t know.
The same reason strcpy() and most of the other standard C string manipulation functions return a pointer: convenience. The pointer returned (in this case) is just the pointer to the destination string. This allows you to make chained calls to string manipulation functions (though it gets messy quickly). No one is forcing you to use the return value...and in most cases, you probably don''t want to.
More importantly, just because it''s a pointer does not mean you should immediately assume some sort of memory (re)allocation is taking place. That''s a bad habit, one I''m sure you''re not guilty of for most cases, but be sure and not to harbor it for any case.
More importantly, just because it''s a pointer does not mean you should immediately assume some sort of memory (re)allocation is taking place. That''s a bad habit, one I''m sure you''re not guilty of for most cases, but be sure and not to harbor it for any case.
quote: Original post by Raduprv
*shrug*
Then why would it return a pointer???
So you can go:
strcat( strcat( string1, string2 ), string3 );
A better alternative is
strncat
which takes the maximum number of characters to append as the third parameter. Note that this is not the total size of the first argument, i.e the following is wrong:char buffer[100];
strncpy( buffer, "Hello ", sizeof(buffer) );
strncat( buffer, "World", sizeof(buffer) );
Instead you should use:
char buffer[100];
strncpy( buffer, "Hello ", sizeof(buffer) );
strncat( buffer, "World", sizeof(buffer) - strlen(buffer) );
Obviously, even better is using
std::string
:std::string buffer;
buffer = "Hello ";
buffer += "World";
If I had my way, I''d have all of you shot! codeka.com - Just click it.
quote: Original post by RaduprvNothing is wrong with the forum software.
<textarea>
...
</textarea>
Edit: WTF is wrong with this forum software, it kills my code!
Posting <textarea> used to completely screw threads up. It''s been disabled. Use [source] if you want a source box.
quote: Original post by RaduprvMy guess is that it allows this:
*shrug*
Then why would it return a pointer???
strcat(a, strcat(b, c));.
By the way, I think your question mark key is stuck.
That textarea was put there AFTER the edit, I thought it MIGHT work. And when I say there is something wrong with this forum software, maybe I DO know what I am saying?
Also, using 3 ''?'' characters doesn''t mean my keyboard is stucked, it just means that I am very confused about something. A lot of people find it acceptable.
Height Map Editor | Eternal lands
Also, using 3 ''?'' characters doesn''t mean my keyboard is stucked, it just means that I am very confused about something. A lot of people find it acceptable.
Height Map Editor | Eternal lands
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement