Pointers Strings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(){
	
	char *Statement = "Hello! How are you today? I am well, thankyou.";

	char *Replace_statement = "Hello";

	char is_this_state[] = "";

	cout << sizeof( Replace_statement ) << endl;

	for( int i = 0; i < sizeof( Replace_statement ); i++ ){
		is_this_state[ i ] =  *( Statement + i );
	}


	cout << Replace_statement << endl;
	cout << is_this_state << endl;


	getchar();
	return 0;
}



Ok so I enter this code and what I get is a bunch of nonsense. I can't display a
print screen. I'm trying to get replace_statement to copy into is_this_state.
Last edited on
I'd suggest using std::string atleast until you get used to memory management. If for some reason you need to use char arrays, while not being safe, here's one way of accomplishing what I believe is what you're trying to do:

1
2
3
4
5
6
const char *statement = "Hello! How are you today? I am well, thankyou.";
const char *replace_statement = "Hello";
char is_this_state[512];

unsigned int replacement_size = strlen(replacement_statement);
strncpy(is_this_state, statement, replacement_size);
Topic archived. No new replies allowed.