can someone explain what's the use of pointers with an example?
i been reading tutorials and doing practicals but they doesnt really aid me in my understanding.........
Printable View
can someone explain what's the use of pointers with an example?
i been reading tutorials and doing practicals but they doesnt really aid me in my understanding.........
suppose you want to write an ltrim function.
what you do is start with the first byte of the string, read on until
the byte is not a space or is a \0 (Ascii 0 end of string).
You now have a pointer to the start of your new, trimmed string.
I'm breaking apart some lines to make it easier to comment.
It makes less efficient code, but easier to understand
Code:void ltrim(char *src, char *dest){
char *buf; // here is my pointer
buf = src; // pointer now aimed at the start of the src string
while(*buf){ // if the character buf is on is anything but
a null char - a zero. stops loop at the end
a string string
if(*buf>32) break; // we found a character that is not
a space or a control character
buf++; // move the pointer over one byte
}
dest = buf; // the destination string now starts with
a non-space character. if the src string was all
spaces, then string is now zero-length
}