string - strcpy 2D Array problems in functions -
may ask wrong code? strcpy seems working inside function. when i'm passing function, first array prints okay other ones don't print correctly?
what seems problem code , correct way?
here code:
void copystring(char *data, int ctr){ int i; char constdata[10][50] = {{"hello"}, {"goodbye"}, {"konichiwa"}, {"sayonara"}, {"ni hao"}, {"zai jian"}, {"annyeong haseyo"}, {"annyeonghi gaseyo"}, {"bonjour"}, {"au revoir"}}; char temp[50][100]; strcpy(temp[ctr], constdata[ctr]); if (ctr == 4) for(i = 0; <=ctr; i++) printf("in function: %s\n", temp[i]); strcpy(&data[ctr], temp[ctr]); } int main() { int = 0, ctr = 0; char data[20][10]; (ctr = 0; ctr <= 4; ctr++) copystring(data[ctr], ctr); printf("\n"); for(i = 0; <= 4; i++) printf("%s\n", data[i]); return 0; }
the output gave me this:
in function: hello
in function: goodbye
in function: konichiwa
in function: sayonara
in function: ni hao
hello
a
a
but output should this:
in function: hello
in function: goodbye
in function: konichiwa
in function: sayonara
in function: ni hao
hello
goodbye
konichiwa
sayonara
ni hao
thank in advance!
here culprit:
strcpy(&data[ctr], temp[ctr]);
this should be:
strcpy(data, temp[ctr]);
Comments
Post a Comment