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

Recommended Answers

All 2 Replies

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

Mhhh!

yeah, very strange.
It turns out it works for me now too...:confused:
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.