What is the meaning of:
char t[20];
char *pt;
pt=t;
...
for (pt=pt+(strlen(t)-1);pt>=t;pt--)
...
These are pointers what is this line of code saying??...in plain english please!!
What is the meaning of:
char t[20];
char *pt;
pt=t;
...
for (pt=pt+(strlen(t)-1);pt>=t;pt--)
...
These are pointers what is this line of code saying??...in plain english please!!
1) char t[20];
Declare an array of characters, 20 characters in length, named 't'.
2) char *pt;
Declare a pointer to a character, and call it 'pt'.
3) pt=t;
Make 'pt' point to the array 't'. Recall that you can use the name of an array as a pointer to the first element in the array.
3) for(
Begin looping.
4) pt=pt+(strlen(t)-1);
Make 'pt' point to the end of the string. We do this by getting the length of the string, by using 'strlen', and subtracting one.
If we didn't subtract one, we'd end up pointing past the string. Much like when you index an array, you subtract one.
5) pt>=t;
As I mentioned before, you can use the name of an array as a pointer to the first element of the array. What we're doing here is saying, "loop until our pointer is pointing back before the first element in the array.
6) pt--)
Decrement the pointer so that it points to the space in memory one place before it's current location.
Quzah.
Hope is the first step on the road to disappointment.
Thanks