GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — C++ cstring memchr() Function

The C++ Standard Library
  • The C Standard Library
  • C++ Library - Home
  • C++ Library - <fstream>
  • C++ Library - <iomanip>
  • C++ Library - <ios>
  • C++ Library - <iosfwd>
  • C++ Library - <iostream>
  • C++ Library - <istream>
  • C++ Library - <ostream>
  • C++ Library - <sstream>
  • C++ Library - <streambuf>
  • C++ Library - <atomic>
  • C++ Library - <complex>
  • C++ Library - <exception>
  • C++ Library - <functional>
  • C++ Library - <limits>
  • C++ Library - <locale>
  • C++ Library - <memory>
  • C++ Library - <new>
  • C++ Library - <numeric>
  • C++ Library - <regex>
  • C++ Library - <stdexcept>
  • C++ Library - <string>
  • C++ Library - <thread>
  • C++ Library - <tuple>
  • C++ Library - <typeinfo>
  • C++ Library - <utility>
  • C++ Library - <valarray>
  • The C++ STL Library
  • C++ Library - <array>
  • C++ Library - <bitset>
  • C++ Library - <deque>
  • C++ Library - <forward_list>
  • C++ Library - <list>
  • C++ Library - <map>
  • C++ Library - <multimap>
  • C++ Library - <queue>
  • C++ Library - <priority_queue>
  • C++ Library - <set>
  • C++ Library - <multiset >
  • C++ Library - <stack>
  • C++ Library - <unordered_map>
  • C++ Library - <unordered_set>
  • C++ Library - <unordered_multiset>
  • C++ Library - <vector>
  • C++ Library - <algorithm>
  • C++ Library - <iterator>
  • The C++ Advanced Library
  • C++ Library - <any>
  • C++ Library - <barrier>
  • C++ Library - <bit>
  • C++ Library - <chrono>
  • C++ Library - <cinttypes>
  • C++ Library - <clocale>
  • C++ Library - <condition_variable>
  • C++ Library - <coroutine>
  • C++ Library - <cstdlib>
  • C++ Library - <cstring>
  • C++ Library - <cuchar>
  • C++ Library - <charconv>
  • C++ Library - <cfenv>
  • C++ Library - <cmath>
  • C++ Library - <ccomplex>
  • C++ Library - <expected>
  • C++ Library - <format>
  • C++ Library - <future>
  • C++ Library - <flat_set>
  • C++ Library - <flat_map>
  • C++ Library - <filesystem>
  • C++ Library - <generator>
  • C++ Library - <initializer_list>
  • C++ Library - <latch>
  • C++ Library - <memory_resource>
  • C++ Library - <mutex>
  • C++ Library - <mdspan>
  • C++ Library - <optional>
  • C++ Library - <print>
  • C++ Library - <ratio>
  • C++ Library - <scoped_allocator>
  • C++ Library - <semaphore>
  • C++ Library - <source_location>
  • C++ Library - <span>
  • C++ Library - <spanstream>
  • C++ Library - <stacktrace>
  • C++ Library - <stop_token>
  • C++ Library - <syncstream>
  • C++ Library - <system_error>
  • C++ Library - <string_view>
  • C++ Library - <stdatomic>
  • C++ Library - <variant>
  • C++ STL Library Cheat Sheet
  • C++ STL - Cheat Sheet
  • C++ Programming Resources
  • C++ Programming Tutorial
  • C++ Useful Resources
  • C++ Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

C++ cstring memchr() Function



Previous
Quiz
Next

The 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.

Syntax

We use the following syntax to use the memchr() function −

void *memchr(const void *str, int c, size_t n);

Parameters

Parameters of the memchr() function are as follows −

  • str: This is the pointer to the memory block where the search is performed.
  • c: This is the character to be located. It is passed as its int promotion.
  • n: This is the number of bytes to be analyzed.

Return Value

The 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 1

Following 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;
}

Output

Following is the output of the above C++ program:

Character found: a

Example 2

Now, 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;
}

Output

Following is the output of the above C++ program −

Character not found
Tutorix - AI Tutor
Previous Quiz Next
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.