×

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks.

  • The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the character that will be filled with another value from the memset () function.
  • The letter "ch" is then copied to the first characters "n" of the object when st [points].
  • In this context, "n" refers to the block size, specified in memset (), and must be equal to or less than the size of the object identified by st [].
  • The effect cannot be explained if the value of n is greater than the size of the object mentioned st [].
  • The function will behave the way you did in the previous example, that is, unspecified, if the situation sometimes occurs when an object cannot be copied.
  • The cstring title file contains a memset () function in the C ++ editing language; without mentioning this header file, you will not be able to use memem () method.

Example1:

// Program to implement memset ( ) function in C++  
#include < iostream >  
#include < bits/stdc++.h >
#include < stdio >
#include < sdtlib >
#include < cstring >      // header file that contains the memset ( ) function, without   
using namespace std ;  // we cannot able to access the use of memset ( ) function
int main ( )  
{  
    char st [ ] = " TutorialAndexample " ;  
    char ch ;  
    cout << " Printing the given string : " << st << endl ;  
    cout << " enter an alphabet from which you want to replace the each charater of the         given string : " ;  
    cin >> ch ;  
    memset ( st , ch , sizeof ( st ) ) ;  
    cout << " Printing the string after replacing the each character of the string with the given substitute : " << endl ;  
    cout << " " << st ;  
    return 0 ;  
}

OUTPUT:

Print the given string: TutorialAndexample
enter an alphabet from which you want to replace the each character of the given string : a
Printing the string after replacing the each character of the string with the given substitute : aaaaaaaaaaaaaa
………………………………………………………
Process executed in 1.11 seconds
Press any key to continue

Explanation

In the aforementioned example, we saw how each letter in the string "JavaTpoint" was changed into the single alphabet supplied by the user. In this case, the space is also regarded as one character.

Example2:

// Program to implement memset ( ) function in C++  
#include < iostream > 
#include < bits/stdc++.h >
#include < stdlib >
#include < stdio > 
#include < cstring >    // header file that contains the memset ( ) function, without it   
using namespace std ;  //we cannot able to access the use of memset ( ) function 
int main()  
{  
    int arr [ 100 ] ;  
    int n , i , p ;  
    cout << " enter size of an integer array: " ;   
    cin >> n ;  
    cout << " enter an array elements: " << endl ;  
    for ( i = 0 ; i < n ; i + + )  
    {  
        cin >> arr [ i ] ;  
    }  
    cout << " Printing the given array : " << endl ;  
    for ( i = 0 ; i < n ; i + + )  
    {  
        cout << " " << arr [ i ] << endl ;  
    }  
    cout << " enter an integer value from which you want to replace the each charater of the given string :  " ;  
    cin >> p ;  
    memset ( arr , p , sizeof ( arr ) ) ;  
    cout << " Printing the array after replacing the each value of the array with the given substitute : " << endl ;  
    for ( i = 0 ; i < n ; i + + )  
    {  
        cout << " " << arr [ i ] << endl ;  
    }  
    return 0 ;  
}

OUTPUT:

enter size of an integer array: 6
enter an array elements: 
12
11
10
55
34
321
Printing the given array:
12
11
10
55
34
321
enter an integer value from which you want to replace the each charater of the given string : 1
Printing the array after replacing the each value of the array with the given substitute :
1
1
1
1
1
1
……………………………..
Process executed in 4.33 seconds
Press any key ton continue.

Explanation

Here, the use of just the integer values 0 and -1 is significant, meaning that all characters in the string must be changed to these values in order to avoid producing trash. If you enter 2, then all the characters in the string have been replaced by a garbage value.


Related Topics

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Lexicographically Next Permutation in C++

In this tutorial, we'll look at how to use C++ to generate the lexicographically next permutation of a string. The lexicographically next permutation is the larger permutation. "ACB," for example,...

3 minutes read.

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.