Thread: Pointer walk

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Pointer walk

    This is how you increment a pointer to read the string from a certain point.

    Now how should i go to do the same with an array like response[255] ?

    Should i use
    Code:
    pos = str.find("live");        // position of "live" in str
    str3 = str.substr (pos);   // get from "live" to the end
    or is there anything better?

    Code:
    int main()
    {
        char * response = "c:\\hello world";
        for(int i=0;i<3;i++) *response++;
        printf("%s\n", response);
    
        return 0;
    }
    Last edited by Ducky; 04-04-2010 at 08:17 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Ducky View Post
    Should i use
    Code:
    pos = str.find("live");        // position of "live" in str
    str3 = str.substr (pos);   // get from "live" to the end
    or is there anything better?
    there are no such things in C. It is C++

    So start with deciding which language you want to use.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Yeah well, sorry, maybe i should have asked: how to do this in C? Thanks.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    look at the strstr function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thanks Vart!

    Code:
        char str[] ="This is a simple string";
        char * pch;
        pch = strstr (str,"simple");
        printf("%s",pch);
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM