I am a novice, I encountered a little problem today, I hope to get someone's answer, thank you very much. Code show as below。
#include<iostream>
using namespace std;
void sub(char b[]){
b[] = "world"; //I alse try b*/b,but it is not ok
}
int main(void){
char a[10] = "hello";
sub(a);
cout<<a<<endl;
system("pause");
return 0;
}
error: expected primary-expression before ']' token b[] = "world"; ^ The error
I want the final output will be "world". The function sub() can run correctly. What should I do?
strcpyto write into an existingchararray (although you'd need more safety checks in a real program)std::strcpyand friends. Plus, if this is really C++, then usestd::strings instead ofchar[].b[] = "world"; //I alse try b*/b,but it is not ok-- You can't learn C++ properly by randomly trying things. C++ is one of the most difficult languages to learn, and it is best to learn it by utilizing peer-reviewed C++ books.