pointers question

This is a discussion on pointers question within the C Programming forums, part of the General Programming Boards category; Code: int strlen(char *s) { int len = 0; while(*s) s++, len++; return len; } This is my own strlen ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    pointers question

    Code:
    int strlen(char *s)
    {
         int len = 0;
         while(*s) 
            s++, len++;
         return len;
    }
    This is my own strlen function...

    Now i was wandering why do some people do;

    char *p = s;
    and then use the p pointer in while iteration instead of s itself.
    Code:
    int strlen(char *s)
    {
         int len = 0;
         char *p = s;
         while(*p) 
            p++, len++;
         return len;
    }
    What's the diffrence?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Perhaps because that way they only need to increment one variable in the loop, and after the loop perform a subtraction.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Also one more question... on theory.

    When this function is invoked in main:
    Code:
    int strlen(char *s)
    {
         int len = 0;
         while(*s) 
            s++, len++;
         return len;
    }
    Does the function itself create a new pointer (in this case s) on stack which then points to the passed adress?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    BTW, argument "s" should probably be pointer to const char instead of just pointer to char.

    Just a nit...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by Tool
    Does the function itself create a new pointer (in this case s) on stack which then points to the passed adress?
    I would not say "the function itself", but roughly: yes. Parameters are local variables.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    And those parameter variables are declared (created) at each call of the function?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by Tool
    And those parameter variables are declared (created) at each call of the function?
    Space is allocated on the stack for them on each function call.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    It is also possible to implement the function as...
    Code:
    int strlen(const char* s)
    {
        const char* p = s;
        while(*p++) ;
        return p - s - 1;
    }
    ...showing why it might be useful to declare an extra variable that can holds s's value.
    In your examples, however, they are merely style.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 05:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21