GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C++ cstring memchr() Function
Selected Reading
C++ cstring memchr() FunctionThe C++ cstring memchr() function is used to search the first occurrence of a character in the first n bytes of the memory block that is pointed by the argument str. Consider a case you need to search name of students, that starts with 'A' in a memory block. In this case, you can use the memchr() function. If you search for a character that is not present in the memory block, the function will return NULL. If memory block, pointed by ptr does not contain at least n bytes, the results are undefined. SyntaxWe use the following syntax to use the memchr() function − void *memchr(const void *str, int c, size_t n); ParametersParameters of the memchr() function are as follows −
Return ValueThe memchr() function returns a pointer to the first occurrence of the character c in the memory block, or NULL if the character does not occur in the given memory block. Example 1Following example demonstrates the basic usage of the memchr() function. In this example we will take a memory block str and then search for the first occurrence of the character 'A' in the first 5 bytes of the memory block using the memchr() function. #include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "tutorialspoint";
char *ret;
ret = (char*)memchr(str, 't', 5);
if(ret) {
cout << "Character found: " << *ret << endl;
} else {
cout << "Character not found" << endl;
}
return 0;
}
OutputFollowing is the output of the above C++ program: Character found: a Example 2Now, let's take another example where we will search for the character that is not present in the memory block. For this, we will take a memory block str and then search for the first occurrence of the character 'z' in the first 5 bytes of the memory block using the memchr() function. #include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "tutorialspoint";
char *ret;
ret = (char*)memchr(str, 'z', 5);
if(ret) {
cout << "Character found: " << *ret << endl;
}else {
cout << "Character not found" << endl;
}
return 0;
}
OutputFollowing is the output of the above C++ program − Character not found Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |