• Advertisement

Archived

This topic is now archived and is closed to further replies.

C String Manipulation

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

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]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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

Share this post


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

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]

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
quote:
Original post by Raduprv
<textarea>
...
</textarea>
Edit: WTF is wrong with this forum software, it kills my code!
Nothing is wrong with the forum software.
Posting <textarea> used to completely screw threads up. It''s been disabled. Use [source] if you want a source box.
quote:
Original post by Raduprv
*shrug*
Then why would it return a pointer???
My guess is that it allows this:
strcat(a, strcat(b, c));.
By the way, I think your question mark key is stuck.

Share this post


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

Share this post


Link to post
Share on other sites
Sigh...
quote:
Original post by Raduprv
And when I say there is something wrong with this forum software, maybe I DO know what I am saying?
Nope. This forum, which allows html, isn''t going to go out of its way to figure out whether to interpret your post as plain text or html. If you want a < symbol (outside of [code] or [source] tags), you do it the html way: either put a whitespace character after it, or type &lt; instead. But using [code] / [source] is usually better for posting code.
quote:
Original post by Raduprv
Also, using 3 ''?'' characters doesn''t mean my keyboard is stucked...
sarcasm
quote:
Original post by Raduprv
A lot of people find it acceptable.
A lot of people don''t.

I don''t like to sound rude, but you seem to be making way too many assumptions.

Share this post


Link to post
Share on other sites
"A lot of people don''t."

aaaaand last time i looked you''re (1) not the mod (2) not god and (3) in no position to judge anyone else.

accept other people for who they are and how they write. it''s part of life.

----------------------------

hey, i know how to use DX8 now! sort of.. well i''ve got the object loading, rotation, positioning, camera, text and lights down... now for input and sound.

Share this post


Link to post
Share on other sites
CP, a char* is the essence of a C string. In C, a string nothing more than an array of characters, with a terminating null delimiter on the end (if it''s well-formed). So there''s no conversion from a char* to a C string, a char* is a C string.

...
I don''t know why strcat returns a pointer to the first string. Several C string functions behave this way (atoi comes to mind). I wish they behaved like printf, and returned the number of characters written. Few C functions allocate memory and return it. It''s also regarded as poor design to do so because of the ambiguity over who owns the memory, and who is responsible for deallocating.

...
I think being polite is more important that perfect grammar. Three question marks won''t motivate me to edit a post like all caps does - thirty questions marks might though.

Please focus on the technical issues, and take the offhanded criticism in stride (ignore them if they bother you).

When an issue rest on solid facts, you ought to take a moment to verify yourself before posting. If you are not certain, please make this clear in the post.

...
quote:

accept other people for who they are and how they write. it''s part of life.



This is a tenuous position to take. If the writer will not take the time to write effectively, why should the many-fold readers take the time to read and perhaps respond?

Share this post


Link to post
Share on other sites

  • Advertisement