memset和sprintf用法

;
class mychar {
public:
int a;
char b[20];


};
int main()
{
mychar  mytest[5];
memset(mytest, 0, sizeof(mytest));    //中间的0表示字符串的\0,如果是23,那么就是很奇怪的字符

cout << mytest[3].a<< "|"<< mytest[3].b << endl;

}


 上面的sizeof 也可以是 mychar, 但是只会初始化mytest[0] 这个变量



char* who = "I";
char* whom = "CSDN";
sprintf(s, "%s love %s.", who, whom); //产生:"I love CSDN. " 这字符串写到s中

sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"

你可能感兴趣的:(C++)