Okay so after searching and not seeing the answer I have decided to make a question with my own answer. Feel free to critique me, but I felt this could help a lot of people with questions about pointers.
As the title suggest this will be going over how to transverse a char array, string from here on out, using a pointer.
I'm making this question because I forgot how to transverse a string using a pointer, and it took me a minute to rediscover how to do it.
This code should work in both c and c++. I have compiled it using ideone.com and I know it works.
#include <stdio.h>
int main() {
char *ptr;
char arr[] = "abcde";
for(ptr = arr; *ptr;ptr++)
{
printf("%c ", *ptr);
}
return 0;
}
http://ideone.com/e.js/CC5IZ6
As we can see above, we first make a pointer of type char, *ptr, and then an array of characters in the char arr[]. Then we have 'ptr' point to 'arr' in the first part of the for loop.
The second part of the for loop, is the conditional section, and we are looking for the end of the string using '\0'. By 'de-referencing' 'ptr' using the '*' in front of the variable we can access the information in memory that the pointer is 'pointing' to, which will be one of the characters in the char array. In this case we are looking for the end of the string signified by the '\0' character. I have heard it called many things; however, in C you call it the null character. But I digress, the next part of the for loop is the increment portion which increments the ptr one position each iteration of the for loop.
The for loop allows us to move the pointer by incrementing memory spots that have been allocated for the array of characters. This allows us to use the pointer to move through the array using pointer arithmetic.
To print the items in the array using printf we need to dereference the pointer once again using the '*' symbol(not sure how best to describe it, so if anyone knows a better way please let me know). This gets the item in memory the pointer is currently pointing at and allows it to print it out.
Now what would happen if we didn't use the '*' symbol? Well, the way printf is currently set up, which is to print chars, we will get an error or nothing at all. If you want to see what memory location the pointer is at, and not what is in that memory location, then change printf to 'printf(%p',ptr);' '%p' means print pointer address.
if you are curious please look here for other commands you can send to printf.
http://www.cplusplus.com/reference/cstdio/printf/
The result of this is the array being printed out in order "a b c d e".
The reason why printer arithmetic is so helpful is for two reasons.
1. Helps with students understanding of pointers, when you are first starting out, before getting in to advanced c++ items.
2. It makes it possible to use dynamic memory, without pointers and the ability to transverse them, you would have no recourse if your program grew past the allocated amount of memory you first allocated for the program.
That last part is my understanding of pointers in general, I'm sure a guru of c will have a better definition and I'll be happy to modify the reasons.
Please let me know what you think, and feel free to give me an critiques, and I'll be happy to modify this question/answer so we can help members of SO.
Thank you,