If we have a char *hello – and the string is "hello"
and i do
char *ptr;
ptr = hello;
then ptr will be pointing at 'h', correct?
Now I have just done an assignmnet in this and completed it using the following terms
if i wanted to move the pointer to the next chatachter i would just do ptr++. If i wanted to use the value of the pointer for some check, i would use if(*ptr == '\0')...
When i was doing the assignmnets our teacher gave us some pre built methods, and they used stuff like
*string++ = *s++;
ok, so why would we want to do *string (which gets a value) – and combine it with ++
I hope i make sense in explaining what is not clear. Its just I managed to do the whole assignment with ptr++ to move to next element or *ptr to check its value
The idiom
*s++means “take the value pointed to, and switch to the next one”.This way you can do your check operations in a loop. The assignment
*p++ = *q++copies the value of*qto the place pointed byp, and shifts bothpandqto the next place, so the next time you execute*p++ = *q++the next character will be copied behind the first one. And so on.