| | |
char arrays and strcpy trouble
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hi,
the following code works fine, that is, strcpy effectively copies the whole string on temporal to word.
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.
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
the following code works fine, that is, strcpy effectively copies the whole string on temporal to word.
cpp Syntax (Toggle Plain Text)
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.
cpp Syntax (Toggle Plain Text)
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
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
strange,
works perfectly for me
C++ Syntax (Toggle Plain Text)
#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
![]() |
Similar Threads
- Order of precedence (C)
- Trouble with Pointers and Arrays (C)
- Is there a way to tokenize an array of strings (C++)
- HELP me as soon as possible ..! (C++)
- total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast" (C++)
- Sorting arrays of pointers with function? (C)
Other Threads in the C++ Forum
- Previous Thread: ARRAYS
- Next Thread: help in function to return the number of items greater than certain number
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





