c++ - strcpy on a string pointer gives errors -


i have read lot subject , confused .

what used work in c file ,not working on cpp file :

 char  *builtinfunctions[20]; 

then error on strcpy function here :

void intepreter::setbuiltins(char *builtins) {    strcpy(builtinfunctions, builtins); // no matching function call strcpy } 

i don't understand basics here, why in c++ not work ( need use = instead ? )

strcpy(char *, const char*) = thats structure 

if change builtinfunctions being pointer works.

edit: reason being const before edit read here : why conversion string constant 'char*' valid in c invalid in c++

that char *builtinfunctions[20]; produce warning when :

builtinfunctions[0]="me"; 

and did. fix removing const .

this array of pointers char.

char  *builtinfunctions[20]; 

so call

strcpy(builtinfunctions, builtins); 

gets treated strcpy(char **, char*), not strcpy(char *dest, const char *src). mismatch first parameter type.

edit:

so let's suppose builtinfunctions "an array of words" wish populate, void intepreter::setbuiltins(char *builtins) meant it's first parameter being pointer new incoming word. (and you're doing in c-style manner. well, you.)

some things consider.

  1. if declare array type arrname[n]; array's name being used alone without index treated variable of type type *arrname. if type char *, builtinfunctions of type char**. that's why strcpy fails, strcpy(builtinfunctions[someindex], builtins); works.

  2. before invoking strcpy should consider, if have destination space allocated. builtinfunctions[someindex] of type char *. point to? valid pointer allocated space, or gateway hell of undefined behaviour strcpy happily take to?


Comments