I wanted to declare an array with a pointer in character type, and the length of the array can be determined by my input string.
I wrote it in this way:
char *s;
cout << "Enter a string: " << endl;
cin >> s;
I expected that I can initialize the string by the cin operation, but an error showed up when compiling. The error is about "invalid operands to binary expression".
I'm not sure why the lines I wrote was wrong.
I though not only the built in string class is used for declaring an array.
Isn't the string data type in C++ the same as "a character array"?
Isn't the line char *s means the pointer s points to an character array (or string)?
Thank you!
You should use std::string.
It is a class that represents a string of characters. It is different than an old c style array of characters (although internally might contain one).
In your case:
#include <string>
#include <iostream>
std::string s;
std::cout << "Enter a string: " << endl;
std::cin >> s;
Using std::string means memory is managed automatically for you. Specifically with cin it will also be resized to fit the input.
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
"the cin operation".
cin is really the source. The real work is done by the overloaded operator>>. And the operator>> which reads to a char* expects that the char* is already allocated to the right size. That's of course a problem with cin, where the size is unknown.
The operator>> overload that reads to std::string will resize the std::string to the right size.
The answer to your question is no, as when you create a type pointer you always have to specify in advance how much memory to allocate. We can imagine that this is what happens with strings, that is to go to fetch the data and arrange the occupied cells in memory at a later time.
Now the real problem is, it is true that you have declared a pointer to a character, but you have not specified how much to allocate for it. It is as if you are saying you want to create a box but you are not specifying the size. I show you the correct method:
char *s = new char[10];
Obviously when using pointers, always remember to deallocate them at the end of use so as not to have any memory leaks.
Taking a summary of the situation, you tried to save a data in a box that you intend to create but does not exist. That is, you have named the box called s which will contain a pointer to a character but you have not yet built/created it in its final size.
Related
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;
}
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.
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.
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";
}
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
You can use char[] instead of string
include <iostream>
using namespace std;
int main()
{
char tmp[101];
scanf("%100s", tmp);
cout << tmp;
}
Here is the code:
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
};
int main ()
{
string mystr;
movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;
cout << "Enter title: ";
getline (cin, pmovie->title);
cout << "Enter year: ";
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;
cout << "\nYou have entered:\n";
cout << pmovie->title;
cout << " (" << pmovie->year << ")\n";
return 0;
}
Taken from http://www.cplusplus.com/doc/tutorial/structures/. I was hoping I could get clarification on a few things.
What is getline and how does it work? I tried looking up the documentation, but I'm still having trouble understanding. Also, what exactly is cin and how is it being used with getline?
If I understand correctly, pmovie->title essentially says that pmovie points to the member title of the object amovie? If so, and it's not already clear from the explanation to #1, how does getline (cin, pmovie->title) work?
Now this (stringstream) mystr >> pmovie->year is giving me the most trouble. What is a stringstream, and are we using it like we would cast a double as a int, for example?
Thank you all!
What is getline and how does it work? I tried looking up the documentation, but I'm still having trouble understanding. Also, what exactly is cin and how is it being used with getline?
The getline function reads a line from a istream. The cin stream refers to your standard input stream, the one you would normally get input from. It is being passed to getline to tell it which input stream to get a line from.
If I understand correctly, pmovie->title essentially says that pmovie points to the member title of the object amovie? If so, and it's not already clear from the explanation to #1, how does getline (cin, pmovie->title) work?
The getline functions reads a line from cin and stores it in pmovie->title which is passed by reference.
Now this (stringstream) mystr >> pmovie->year is giving me the most trouble. What is a stringstream, and are we using it like we would cast a double as a int, for example?
A stringstream is a class that makes a string act like a stream. This is kind of confusing syntax (C-style cast) that makes it a bit harder to understand what it is happening. Basically, a temporary stringstream is created and initialized with the contents of mystr. A stringstream, when initialized with a string, gives you a stream from which you can read those contents. The >> operator reads from an output stream, in this case, into pmovie->year, which is again passed by reference.
By the way, it seems to me like you're trying to understand unusually complex and confusing uses without yet understanding the more basic uses of these objects. That's a very hard way to learn.
Most of the questions don't seem to be about structures at all. So, I'm addressing the issue which is related to the title rather than those about streams:
If I understand correctly, pmovie->title essentially says that pmovie points to the member title of the object amovie? If so, and it's not already clear from the explanation to #1, how does getline (cin, pmovie->title) work?
You misunderstand. I would guess, that this is the root of your confusion: pmovie points to a movies_t object. As it happens, in the sample code it is initialized to point to the movies_t object named amovie.
Now, each movies_t object has two members, i.e., subobjects: a title and a year. To access the title component of a movies_t pointed to by a pointer you use pmovie->title. To access the year component instead you'd use pmovie->year.
The one thing I say about streams, though, is this: You should always check that your input was successful before assuming the read was successful. For example, you would check that reading a line was successful using
if (std::getline(std::cin, pmovie->title)) {
// deal with a successfully read title
}
cin is a special stream defined by C++ to work with standard output (usually the keyboard, but can be almost anything). getline is a function that allows you to read text from a stream into a buffer until the platform's line ending is encountered (Line Feed on UNIX, Carriage Return Line Feed of Windows and DOS).
pmovie->title says that pmove is a pointer to a structure that has a member called title. This refers to that member. Because getline takes a string& (String reference), it happily accepts the string referenced by pmovie->title.
stringstream defines an implicit constructor that converts strings to stringstreams. the >> operator gets input from a string and converts it to the target type (the type of the operand to the right of the >>) and puts it there. This is just a way of converting a string to an integer.
The following code seems to be running when it shouldn't. In this example:
#include <iostream>
using namespace std;
int main()
{
char data[1];
cout<<"Enter data: ";
cin>>data;
cout<<data[2]<<endl;
}
Entering a string with a length greater than 1 (e.g., "Hello"), will produce output as if the array were large enough to hold it (e.g., "l"). Should this not be throwing an error when it tried to store a value that was longer than the array or when it tried to retrieve a value with an index greater than the array length?
The following code seems to be running when it shouldn't.
It is not about "should" or "shouldn't". It is about "may" or "may not".
That is, your program may run, or it may not.
It is because your program invokes undefined behavior. Accessing an array element beyond the array-length invokes undefined behavior which means anything could happen.
The proper way to write your code is to use std::string as:
#include <iostream>
#include <string>
//using namespace std; DONT WRITE THIS HERE
int main()
{
std::string data;
std::cout<<"Enter data: ";
std::cin>>data; //read the entire input string, no matter how long it is!
std::cout<<data<<std::endl; //print the entire string
if ( data.size() > 2 ) //check if data has atleast 3 characters
{
std::cout << data[2] << std::endl; //print 3rd character
}
}
It can crash under different parameters in compilation or compiled on other machine, because running of that code giving undefined result according to documentaton.
It is not safe to be doing this. What it is doing is writing over the memory that happens to lie after the buffer. Afterwards, it is then reading it back out to you.
This is only working because your cin and cout operations don't say: This is a pointer to one char, I will only write one char. Instead it says: enough space is allocated for me to write to. The cin and cout operations keep reading data until they hit the null terminator \0.
To fix this, you can replace this with:
std::string data;
C++ will let you make big memory mistakes.
Some 'rules' that will save you most of the time:
1:Don't use char[]. Instead use string.
2:Don't use pointers to pass or return argument. Pass by reference, return by value.
3:Don't use arrays (e.g. int[]). Use vectors. You still have to check your own bounds.
With just those three you'll be writing some-what "safe" code and non-C-like code.
#include <iostream>
using namespace std;
typedef struct
{
char streetName[5];
} RECORD;
int main()
{
RECORD r;
cin >> r.streetName;
cout << r.streetName << endl;
}
When I run this program, if I enter in more than 5 characters, the output will show the whole string I entered. It does not truncate at 5 characters. Why is that?
How can I get this to work correctly?
You are overflowing the buffer. Put another char array after streetName and you will likely find that it gets the rest of the characters. Right now you are just corrupting some memory on your stack.
In order to limit the input to the size of the receiving array you need to use the length-limiting facilities provided by your input method. In your case you are using cin, which means that you can specify the limit by using its width method
cin.width(5);
cin >> r.streetName;
Because cin sees streetName as a char * and writes to memory and there is nothing to stop writing to *(streetName + 6) and further. This is a form of buffer overrun
The best code in this case is define streetName as a std::string
i.e.
typedef struct
{
std::string streetName;
} RECORD;
Because you're overruning the end of your buffer and in this particular case you're getting away with it. C and C++ make it very easy to "shoot yourself in the foot", but that doesn't mean that you should.
It's a buffer overrun.
C++ does not perform bounds checking on array accesses, and memory does not simply stop at the end of the array. You are writing data to memory that is not part of the array, the consequences of which are non-deterministic, and may sometimes even appear to work.
It is quite likely that if you placed that code into a function, the program would crash when you tried to return from the function, because one likely possibility is that you will have dumped on the function return address on the stack. You may also have corrupted data belonging to the calling function.
The way to do this correctly in c++ is to use a std::string.
#include<iostream>
#include<string>
....
std::string r;
getline(cin, r);
std::cout << r <<std::endl;
For truncated input(with suitably defined and inited values).
while(cin.peek() != EOF && i < len)
{
cin >> arr[i];
++i;
}
You will want to do something after this to flush the buffer and not leave the rest of the line sitting on the input stream if you plan on doing other things with it.
I am a newbie to C++ and learning from the MSDN C++ Beginner's Guide.
While trying the strcat function it works but I get three strange characters at the
beginning.
Here is my code
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
char first_name[40],last_name[40],full_name[80],space[1];
space[0] = ' ';
cout << "Enter your first name: ";
gets(first_name);
cout << "Enter your last name: ";
gets(last_name);
strcat(full_name,first_name);
strcat(full_name,space);
strcat(full_name,last_name);
cout << "Your name is: " << full_name;
return 0;
}
And here is the output
Enter your first name: Taher
Enter your last name: Abouzeid
Your name is: Y}#Taher Abouzeid
I wonder why Y}# appear before my name ?
You aren't initializing full_name by setting the first character to '\0' so there are garbage characters in it and when you strcat you are adding your new data after the garbage characters.
The array that you are creating is full of random data. C++ will allocate the space for the data but does not initialize the array with known data. The strcat will attach the data to the end of the string (the first '\0') as the array of characters has not been initialized (and is full of random data) this will not be the first character.
This could be corrected by replacing
char first_name[40],last_name[40],full_name[80],space[1];
with
char first_name[40] = {0};
char last_name[40] = {0};
char full_name[80] = {0};
char space[2] = {0};
the = {0} will set the first element to '\0' which is the string terminator symbol, and c++ will automatically fill all non specified elements with '\0' (provided that at least one element is specified).
The variable full_name isn't being initialized before being appended to.
Change this:
strcat(full_name,first_name);
to this:
strcpy(full_name,first_name);
You can not see any problem in your test, but your space string is also not null-terminated after initializing its only character with ' '.
As others have said, you must initialize the data, but have you ever thought about learning the standard c++ library? It is more intuitive sometimes, and probably more efficient.
With it would be:
string full_name=first_name+" "+last_name;
and you won't have to bother with terminating null characters. For a reference go to cplusplus
Oh and a full working example so you could understand better (from operator+=):
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name ("John");
string family ("Smith");
name += " K. "; // c-string
name += family; // string
name += '\n'; // character
cout << name;
return 0;
}
The problem is with your space text.
The strcat function requires a C-style string, which is zero or more characters followed by a null, terminating, character. So when allocating arrays for C-style strings, you need to allocate one extra character for the terminating null character.
So, your space array needs to be of length 2, one for the space character and one for the null character.
Since space is constant, you can use a string literal instead of an array:
const char space[] = " ";
Also, since you are a newbie, here are some tips:
1. Declare one variable per line.
This will be easier to modify and change variable types.
2. Either flush std::cout, use std::endl, or include a '\n'.
This will flush the buffers and display any remaining text.
3. Read the C++ language FAQ.
Click here for the C++ language Frequently Asked Questions (FAQ)
4. You can avoid C-style string problems by using std::string
5. Invest in Scott Myers Effective C++ and More Effective C++ books.
Strings are null-terminated in C and C++ (the strcat function is a legacy of C). This means that when you point to a random memory address (new char[] variables point to a stack address with random content that does not get initialized), the compiler will interpret everything up to the first \0 (null) character as a string (and will go beyond the allocated size if you use pointer arithmetic).
This can lead to very obscure bugs, security issues (buffer overflow exploits) and very unreadable and unmaintainable code. Modern compilers have features that can help with the detection of such issues.
Here is a good summary of your options.