This declaration
char str[] = "geeksforgeeks";
declares a character array that contains a string that is a sequence of characters including the terminating zero symbol '\0'
.
You can imagine the declaration the following equivalent way
char str[] =
{
'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's', '\0'
};
This call of the function memset
memset(str, 't', sizeof(str));
overrides all characters of the array including the terminating zero.
So the next statement
cout << str << endl;
results in undefined behavior because it outputs characters until the terminating zero is encountered.
You could write instead
#include <iostream>
#include <cstring>
int main()
{
char str[] = "geeksforgeeks";
std::memset( str, 't', sizeof( str ) - 1 );
std::cout << str << '\n';
}
Or the following way
#include <iostream>
#include <cstring>
int main()
{
char str[] = "geeksforgeeks";
std::memset( str, 't', std::strlen( str ) );
std::cout << str << '\n';
}
That is keeping the terminating zero unchanged in the array.
If you want to override all characters of the array including the terminating zero, then you should substitute this statement
std::cout << str << '\n';
for this statement
std::cout.write( str, sizeof( str ) ) << '\n';
as it is shown in the program below because the array now does not contain a string.
#include <iostream>
#include <cstring>
int main()
{
char str[] = "geeksforgeeks";
std::memset( str, 't', sizeof( str ) );
std::cout.write( str, sizeof( str ) ) << '\n';
}
As for this call
memset(str, "t", sizeof(str));
then the type of the second argument (that is the type const char *
) does not correspond to the type of the second function parameter that has the type int
. See the declaration of the function
void * memset ( void * ptr, int value, size_t num );
Thus the compiler issues an error message.
Apart from character arrays (that are used very often even in C++) you can use also the standard class std::string
(or std::basic_string
) that simulates strings.
In this case there is no need to use the standard C function memset to fill a string with a single character. The simplest way to do this is the following
#include <iostream>
#include <string>
int main()
{
std::string s( "geeksforgeeks" );
s.assign( s.length(), 't' );
std::cout << s << '\n';
}
Another way is to use the standard algorithm std::fill
or std::fill_n
declared in the header <algorithm>
. For example
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
int main()
{
std::string s( "geeksforgeeks" );
std::fill( std::begin( s ), std::end( s ), 't' );
std::cout << s << '\n';
}
or
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
int main()
{
std::string s( "geeksforgeeks" );
std::fill_n( std::begin( s ), s.length(), 't' );
std::cout << s << '\n';
}
You even can use the method replace
of the class std::string
one of the following ways
#include <iostream>
#include <string>
int main()
{
std::string s( "geeksforgeeks" );
s.replace( 0, s.length(), s.length(), 't' );
std::cout << s << '\n';
}
Or
#include <iostream>
#include <string>
int main()
{
std::string s( "geeksforgeeks" );
s.replace( std::begin( s ), std::end( s ), s.length(), 't' );
std::cout << s << '\n';
}
"t"
and't'
are not the same. – Barouchememset
in C++? The reason old C functions exists is for backwards compability. – Antisepticizestd::string
instead ofchar[]
– Antisepticizestd::memset
, use std::fill, its just as fast and safer. – Infracostal"geeksforgeeks"
has 13 characters, and that row of t's that represents the output has 14. So the example code produces extra output, too. As you can see from the answers, that's not unexpected -- the code is simply wrong. – Soniferoususing namespace std
- it is a bad habit to get into, and can silently change the meaning of your program when you're not expecting it. Get used to using the namespace prefix (std
is intentionally very short), or importing just the names you need into the smallest reasonable scope. Is it really so hard to writestd::memset
? – Agraffe"t"
in double quotes, and I voted to close it as a typo (clearly memset doesn't accept pointers as it's second argument). And than you edited the question, completely changing it's meaning - not it became a valid answerable question, but doing so, you invalidate previous answers, which is actually not that great. – Barouche