0

Hi,

the following code works fine, that is, strcpy effectively copies the whole string on temporal to word.

char word[20];
char temp[ ] = "hola";
cout << temporal << endl;
strcpy(word, temporal);
cout << word << endl;

However, if I try to use the heap to store the "word" array then I'm in trouble and I get copied only the first character.

char * word = new char [20];
char temporal[ ] = "hola";
strcpy(word, temporal);
cout << word << endl; // it only prints h

The question is what am I doing wrong? Is there a function to do that or do I have to write my own function? There is probably something I don't quite understand about strings but don't know what it is.
Please, any help is appreciated

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by freelancelote
1

strange,

#include <iostream>

using std::cout;
using std::endl;

int main(void) {
	char * word = new char [20];
	char temporal[ ] = "hola";
	strcpy(word, temporal);
	cout << word << endl;
	return 0;
}

works perfectly for me :S

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.