scanf function for strings

-2

The problem is simple, the code below does not work. it says Process finished with exit code -1073740940 (0xC0000374). Removing ampersand does not change anything.

int main(){
    string x;
    scanf("%s",&x);
    cout << x;
}
c++
scanf
asked on Stack Overflow Sep 7, 2019 by (unknown user) • edited Sep 7, 2019 by giusti

5 Answers

2

scanf() with the %s format specifier reads bytes into a preallocated character array (char[]), to which you pass a pointer.

Your s is not a character array. It is a std::string, a complex object.

A std::string* is not in any way the same as a char*. Your code overwrites the memory of parts of a complex object in unpredictable ways, so you end up with a crash.

Your compiler should have warned about this, since it knows that a char* is not a std::string*, and because compilers are clever and can detect mistakes like this despite the type-unsafe nature of C library functions.

Even if this were valid via some magic compatibility layer, the string is empty.

Use I/O streams instead.

answered on Stack Overflow Sep 7, 2019 by Lightness Races in Orbit • edited Sep 8, 2019 by Lightness Races in Orbit
0

You cannot pass complex objects through the ... operator of printf/scanf. Many compilers print a warning for that.

scanf requires a pointer of type char* pointing to sufficient storage for an argument of %s. std::string is something completely different.

In C++ the iostream operators are intended for text input and output.

cin >> x;

will do the job.

You should not use scanf in C++. There are many pitfalls, you found one of them.

Another pitfall: %s at scanf is almost always undefined behavior unless you you really ensure that the source stream can only contain strings of limited size. In this case a buffer of char buffer[size]; is the right target. In any other case you should at least restrict the size of the string to scan. E.g. use %20s and of course a matching char buffer, char buffer[21];in this case. Note the size +1.

answered on Stack Overflow Sep 7, 2019 by Marcel • edited Sep 7, 2019 by Marcel
0

You should use cin. But if you want to use scanf() for whatever reason and still manipulate your strings with std::string, then you can read the C-string and use it to initialize your C++ string.

#include <iostream>
#include <cstdio>
#include <string>

using std::cout;
using std::string;

int main()
{
    char c_str[80];

    scanf("%s", c_str);
    string str(c_str);

    cout << str << "\n";
}
answered on Stack Overflow Sep 7, 2019 by giusti
0

If you want to use strings, use cin (or getline).

string s;
cin>>s;   //s is now read

If you want to use scanf, you want to have a char array (and don't use &):

char text[30];
scanf("%s", text); //text is now read
answered on Stack Overflow Sep 7, 2019 by Daniel
0

You can use char[] instead of string

include <iostream>
using namespace std;
int main()
{
  char tmp[101];
  scanf("%100s", tmp);
  cout << tmp;
}
answered on Stack Overflow Sep 8, 2019 by Nijin P J

User contributions licensed under CC BY-SA 3.0