8

Is there a way to traverse character by character or extract a single character from char* in C?

Consider the following code. Now which is the best way to get individual characters? Suggest me a method without using any string functions.

char *a = "STRING"; 
0

5 Answers 5

23

Another way:

char * i; for (i=a; *i; i++) { // i points successively to a[0], a[1], ... until a '\0' is observed. } 
Sign up to request clarification or add additional context in comments.

11 Comments

Indeed, but you need to then explicitly dereference i inside your for loop which you don't need to do with a[foo]. This could muddy the answer for a beginner.
It can be superior to other ways because no extra addition occurs. With a[i], there is always the addition a+i, where a is a pointer and i an integer. This is inferior to just i++ where i is a pointer if a[i] resp. *i is used multiple times inside the loop. But this is micro-optimization.
@Arafangion: nonsense. Why on earth would a C implementer go out of their way to create that difference between the two?
@MGZero: "++i is also the more efficient one" also nonsense. This is C, not C++, and there's absolutely no need for a compiler to make an unused copy of the prior value of i. ++i could only be more efficient if "most cases" if most code is very feebly optimized, but closely considering the performance of unoptimized code is fairly futile. In C++ it's true that for user-defined types, the optimization is a little harder in general and sometimes actually impossible.
@MinhTran It should not do so. It should only terminate if *i is \0, for obvious reasons (\0 is the null byte, which is == 0 and evaluates to false in boolean contexts).
|
5

Knowing the length of the char array, you can cycle through it with a for loop.

for (int i = 0; i < myStringLen; i++) { if (a[i] == someChar) //do something } 

Remember, a char * can be used as a C-Style string. And a string is just an array of characters, so you can just index it.

EDIT: Because I was asked on the comments, see section 5.3.2 of this link for details on arrays and pointers: http://publications.gbdirect.co.uk/c_book/chapter5/pointers.html

12 Comments

There are many char*'s that can't be used as C-style string. A 'char*' might be pointing to a single char, or a block of data, or anything, really.
That still doesn't stop you from indexing it.
It does, if it only points to a single char. In that context, it might be nonsensical to talk about it in terms of "indexing".
Well, we're not talking about that context, now are we? The original question specifically asked about a string.
Except they are the same as far as the compiler is concerned. publications.gbdirect.co.uk/c_book/chapter5/pointers.html See section 5.3.2, example 5.3 (read the above paragraph). Also, I showed arrays did I not? I didn't even mention pointer arithmetic until you brought it up.
|
4
size_t i; for (i=0; a[i]; i++) { /* do something with a[i] */ } 

Comments

3

Like this.

char a1[] = "STRING"; const char * a2 = "STRING"; char * c; /* or "const char" for a2 */ for (c = aN; *c; ++c) { /* *c is the character */ } 

Here N can be 1 or 2. For a1 you can modify the characters, for a2 you cannot. Note that assigning a string literal to a char* is deprecated.

Comments

2
int i=0; while(a[i]!=0)/* or a[i]!='\0' */ { // do something to a[i] ++i; } 

EDIT:

You can also use strlen(a) to get the number of characters in a.

3 Comments

Wouldn't that while loop go out of bounds?
No, but as it was it wouldn't have moved on from the zeroth character. Edited to include ++i now though.
And it still won't go out of bounds because C strings are null terminated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.