c-style string array and pointer

I was told that if I define

1
2
char *cstrp;
char cstra[c];


then the cstrp can be treated as cstra, and so I can also use

cin>>cstrp;

but when I write the following program, I find it don't work, don't have clue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main() {
	char cstr[5];
	cin>>cstr;
	for(size_t i=0; i<5; i++) {
		cout<<cstr[i]<<'\t';
	}
	cout<<endl;

	char *cstrp;
	cin>>cstrp;
	while(*cstrp) {
		cout<<*cstrp<<'\t';
		cstrp++;
	}
	cout<<endl;

	return 0;
}


for cstr, it work exactly as what I expected, but for cstrp, no matter what I input, with a null terminator or not, I just got nothing printed. why? can we really use cstrp in that way or not? How to use it? Thanks!
Last edited on
Your problem is that cstrp isn't pointing to any memory area that can receive the characters -- i.e., it's uninitialized. This is in contrast with the cstr area, which points to an area of memory that can hold 5 characters.

To use cstrp, you first need to initialize it. For example:

char *cstrp = new char[5];

Then you can do what you're doing.
Don't forget to delete the memory when you're done:

delete[] cstrp;

- jp
Thanks, jpek42, I did what you said but there come up with another question

I changed the code to
1
2
3
4
5
6
7
        char *cstrp=cstr;//works the same as char *cstrp=new char[5];
	cin>>cstrp;
	while(*cstrp) {
		cout<<*cstrp<<'\t';
		cstrp++;
	}
	cout<<endl;


when I input 1234567, all seven numbers will be printed out, instead of the first 5, why?
You should stop doing that (entering more than 4 characters in a 5 bytes buffer) or you will invoke undefined behaviour.
Thanks, modoran! one other question about string size()

1
2
3
4
5
string s2="uvw\0xyz";
	for(string::size_type i=0; i<s2.size(); i++) {
		cout<<s2[i]<<'\t';
	}
	cout<<endl;

this code will only output the first three characters of s2, why? so a string is not the one you can put anything in it?! you got be careful there is no \0? why it is designed like that?
char *cstrp=cstr;//works the same as char *cstrp=new char[5];

No it doesn't. char *cstrp=new char[5]; allocates some new memory on the heap, and sets cstrp to be the address of that new memory.

char *cstrp=cstr; just sets cstrp to be the address of the first element of cstr.

Very different things!

you got be careful there is no \0? why it is designed like that?

Because a std::string uses a C-style character array to store the actual characters of that string. It uses the '\0' in the same way as you would if you were directly using a C string.
Thanks, MikeyBoy, so what is the main difference between string and cstring? the null terminator?

So what this member function, .c_str(), do? only add a '\0' at the end of the string? Thanks.
Some reading for you:
http://www.cplusplus.com/faq/sequences/strings/c-strings-and-pointers/

A std::string is a class that manages a c-string for you. (To put it very simply.)
Topic archived. No new replies allowed.