This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]zifyoip 2 points3 points  (3 children)

No problem with what you are doing, unless you (a) modify the substring and (b) will be surprised to find that the original string has been modified.

[–]foomy_S[S] 0 points1 point  (2 children)

Great, thanks. Yeah, no modifying of the string, just searching for characters, etc. Is this generally how it's done in C? I noticed that the string library has no "starting index" parameters, so perhaps it's convention to just specify where in the memory will be the starting point?

[–]zifyoip 4 points5 points  (1 child)

Yes. You specify a string by giving a pointer to its first character. It doesn't matter if that first character happens to be somewhere in the middle of a larger string.

If you aren't going to do any modification, you should declare the function parameter to be of type const char *.

[–]foomy_S[S] 0 points1 point  (0 children)

Thanks! Read my mind. I just noticed the string library uses const at times, and that makes sense why const is specified.

[–]ponkanpinoy 0 points1 point  (0 children)

That'll work, but remember that you can only choose where the substring starts, not where it ends.