Thread: how strchr works?

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by EVOEx View Post
    Well, an array is a pointer (more or less). So actually it says pointer - pointer. Actually, it returns the number of elements from the second address till the first address (exclusive). Basically, it does (address2 - address1) / sizeof(data_type_of_pointer).

    The link suggested about pointer arithmetic should contain a more extensive explanation.
    thank you very much...i have one more question:

    is there another way? i mean insteed "loc-buf" if there is another way that does the same "job".

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Although pointers are commonly used in C, you can use an index in a very similar way, to do the same job.

    For inexperienced programmers, I believe an index is easier to work with, and to understand:


    Say you wanted to find the first 'r':
    Code:
    int i=0;
    char array[] = {"Singing in the Rain"};
    
    while(array[i]) {  //array[string length of array[] + 1 will be '\0'
      if(array[i] == 'r' || array[i] == 'r')
        break;
      ++i;
    }
    The variable i will now have the element number of the 'R'.

    I'm posting this answer code for you with this understanding - you *will* post up code (or pseudo code), to try and solve your future C programming questions, with your first post in a thread. Don't expect a "codez plz" kind of relationship with the forum.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by Adak View Post
    Although pointers are commonly used in C, you can use an index in a very similar way, to do the same job.

    For inexperienced programmers, I believe an index is easier to work with, and to understand:


    Say you wanted to find the first 'r':
    Code:
    int i=0;
    char array[] = {"Singing in the Rain"};
    
    while(array[i]) {  //array[string length of array[] + 1 will be '\0'
      if(array[i] == 'r' || array[i] == 'r')
        break;
      ++i;
    }
    The variable i will now have the element number of the 'R'.

    I'm posting this answer code for you with this understanding - you *will* post up code (or pseudo code), to try and solve your future C programming questions, with your first post in a thread. Don't expect a "codez plz" kind of relationship with the forum.
    yeah i know that i have to post code...and this is what i am doing...i just don' t know C very well so i have many troubles...and also, i don' t speek english daily (it is not my "first" language)...this is another problem...but i have to learn C and next year C++ for my university so i just try...thanks for your answer.....

  4. #19
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    You're basically looping through an array and checking if an element exists in it. Here's a code example:

    Code:
    int strchr(const char *s, const char c) {
       while (*s) {
             if (*s == c) 
                  return 1; 
             else
                  s++;
         }
    
         return 0;
    }
    Last edited by dxfoo; 06-21-2010 at 05:31 PM. Reason: It's Monday.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, dxfoo's example is fatally flawed due to a misplaced closing brace, but that should be easy to spot and fix (although the resulting code is not quite standard strchr).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Fixed after I posted Simple function, but it explains how the basic algorithm works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-29-2010, 08:02 PM
  2. .DLL works in win2K but not XP
    By phantom in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2010, 04:55 AM
  3. Now it works, now it won't
    By skytreader in forum C Programming
    Replies: 3
    Last Post: 11-23-2009, 09:56 AM
  4. Replies: 1
    Last Post: 08-04-2009, 08:57 AM
  5. Can anybody show me why this works??
    By tzuch in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 09:03 AM