Thread: memchr implementation

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    memchr implementation

    I have a small doubt in the implementation of memchr function implementation in the code shown below.

    [insert]
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void * xmemchr(const void *buffer, int ch, size_t count);
    
    int main(int argc, char *argv[])
    {
    	char *p;
    	char *s = "This is a test";
    
    	//p = memchr(s,'e',14);
    	p = xmemchr(s,'e',14);
    	printf("\n %p \t %s \t %c \n", p, p, *p);
    	return 0;
    }
    
    void * xmemchr(const void *buffer, int ch, size_t count)
    {
    	int i;
    	char c;
    	char *s = buffer;
    	
    	for(i = 0; i< count ; i++)
    	{
    		if(*s == ch){
    			
    			return s;
    		}
    		else
    			s++;
    	}
    }
    In the line
    if(*s == ch)

    i was assuming how can i compare the char contained in s and the int value that ch has or is it that the compiler does this check on its own. I mean how is it possible to compare a char and an integer. Though i got it working but this point is yet unclear to me.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    They're both numbers, aren't they? If they're the same number, then they're equal; if not, they're not.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes they are both numbers in the internal machine language but when i say *s is it not that i am getting the value contained at address in s which is a character for example 'e' in this case.

    My point was that would the check condition like this be wrong

    *s == 'e' for the above case.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    *s is the value that s points to, yes. s is a char*, so *s must be a char, yes.

    So, for instance, if s was "Hello world", then the first time *s would be 72, then 101, then 108, then 108 again, and so on. s is a movable pointer, so s++ makes it point to each character in turn.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Thanks tabstop i get the point and also i tried the following

    *s == 'e' and it seems to work. So my thinking that this should work was correct as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. help in aggregation implementation
    By Bargi in forum C++ Programming
    Replies: 0
    Last Post: 11-01-2007, 03:01 AM
  3. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM