Quick question. When you are accessing a character array, I know you can set the pointer to the first element in the array, and use a while look and do something like
while (*ptr != '\0') {
do something
}
Now is there a double or int equivalent?
#define ARRAY_SIZE 10
double someArray[ARRAY_SIZE] = {0};
double *ptr = someArray;
// then not sure what to do here? I guess I am looking for an equivalent of the above while loop, but don't want to just do:
for (int i = 0; i < ARRAY_SIZE); *ptr++)
cout << *ptr;
thanks!
If I understand you correctly, you want to iterate through the array and stop when *ptr has a certain value. That’s not always possible. With a character array (string), a common convention is to have the string be “null-terminated”; that is, it will have a 0 byte (‘\0’) at the end. You can add such a sentinel to an int or double-valued array (if you can single out a “special” value that won’t otherwise be used), but it’s not a generally applicable technique.
By the way, your for-loop is probably not what you want:
for (int i = 0; i < ARRAY_SIZE); *ptr++)
If you want to iterate through the array, you’ll need to increment the pointer (ptr++), not the value to which it points (*ptr++).