7

Here is what I have:

char* input = new char [input_max]
char* inputPtr = iput;

I want to use the inputPtr to traverse the input array. However I am not sure what will correctly check whether or not I have reached the end of the string:

while (*inputPtr++)
{
    // Some code
}

or

while (*inputPtr != '\0')
{
    inputPtr++;
    // Some code
}

or a more elegant option?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bbvarghe
  • 267
  • 2
  • 5
  • 18
  • They are equivalent. Of course, you must make sure there actually is a `\0` in the array. – juanchopanza Jun 14 '13 at 05:24
  • This is a question of what you put in your array. Are you sure you have an `'\0'` element in your array? If there is, are you sure the `'\0'` element is in the bounds of the array? – Mark Garcia Jun 14 '13 at 05:26
  • I am new to programming so excuse me if what I am about to ask is redundant, but is '\0' == 0? – Bbvarghe Jun 14 '13 at 05:26
  • _"is '\0' == 0"_ - Yep. [Look here](http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0). – awesoon Jun 14 '13 at 05:28
  • @juanchopanza So does that mean that the while statement first checks the value of inputPtr after being dereferenced, and then increments the the memory address stored in inputPtr??? (I am still new to pointers, so I get confused when it comes to this stuff) – Bbvarghe Jun 14 '13 at 05:30
  • Postfix operator `++` increases something and returns the old value. So `*i++` has the effect of de-referencing `i`, *then* increasing its value. – juanchopanza Jun 14 '13 at 05:37

3 Answers3

10

Assuming input string is null-terminated:

for(char *inputPtr = input; *inputPtr; ++inputPtr)
{
  // some code
}

Keep in mind that the example you posted may not give the results you want. In your while loop condition, you're always performing a post-increment. When you're inside the loop, you've already passed the first character. Take this example:

#include <iostream>
using namespace std;

int main()
{
  const char *str = "apple\0";
  const char *it = str;
  while(*it++)
  {
    cout << *it << '_';
  }
}

This outputs:

p_p_l_e__

Notice the missing first character and the extra _ underscore at the end. Check out this related question if you're confused about pre-increment and post-increment operators.

Community
  • 1
  • 1
greatwolf
  • 20,287
  • 13
  • 71
  • 105
2

Assuming input isn't null terminated:

char* input = new char [input_max];
for (char* inputPtr = input; inputPtr < input + input_max; 
        inputPtr++) {
  inputPtr[0]++; 
}   

for the null terminated case:

for (char* inputPtr = input; inputPtr[0]; inputPtr++) {
      inputPtr[0]++; 
}   

but generally this is as good as you can get. Using std::vector, or std::string may enable cleaner and more elegant options though.

perreal
  • 94,503
  • 21
  • 155
  • 181
2

I would do:

inputPtr = input; // init inputPtr always at the last moment.
while (*inputPtr != '\0') {      // Assume the string last with \0
       // some code
       inputPtr++; // After "some code" (instead of what you wrote).
}

Which is equivalent to the for-loop suggested by greatwolf. It's a personal choice.

Be careful, with both of your examples, you are testing the current position and then you increment. Therefore, you are using the next character!

Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • So would a do-while be more appropriate if I stick with my examples? – Bbvarghe Jun 14 '13 at 05:39
  • 1
    A do-while would mean: test after executing some code, and as a consequence, always execute at least one time the loop. That could be useful if you want to consider the `\0` character for instance. But that would not be equivalent to your examples. – Maxime Chéramy Jun 14 '13 at 05:46