Results 1 to 2 of 2

Thread: pointers help!?!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    pointers help!?!

    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.........

  2. #2
    jim mcnamara
    Guest
    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
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width