1.
void countSize()
{
char *str1="hello,snowman";
char str2[]="hello,snowman";
char str3[20]="hello,snowman";
char *str4;
str4 = (char*)malloc(sizeof(char)*20);
strcpy(str4, "hello,snowman");
cout<<"1. "<<sizeof("hello,snowman")<<endl;
cout<<"2. "<<sizeof(str1)<<endl;
cout<<"3. "<<sizeof(str2)<<endl;
cout<<"4. "<<sizeof(str3)<<endl;
cout<<"5. "<<sizeof(str4)<<endl;
return;
}
2.
struct ST
{
int id;
int n;
};
void pointer1()
{
struct ST *ptr;
ptr = (struct ST*)malloc(sizeof(struct ST));
ptr->id=101;
ptr->n=10;
}
void pointer2()
{
struct ST *ptr;
ptr = (struct ST*)malloc(sizeof(struct ST));
free(ptr);
if(ptr!=NULL)
{
ptr->id=101;
ptr->n=10;
}
}
3.
class CBall
{
public:
CBall();
~CBall();
public:
void getBall();
private:
int m_id;
char *m_ballName;
};
class CFootBall:CBall
{
public:
CFootBall();
~CFootBall();
public:
void getBall();
private:
int m_teamId;
string m_country;
};
4.
struct STUDENT
{
int id;
char name[20];
int score[10];
};
void setStudent(struct STUDENT &stu)
{
char myName[30]={"DavidBeckham"};
int score[10]={90,92,96,0};
stu.id=10;
memcpy(stu.score, score, sizeof(score));
strncpy(stu.name, myName, sizeof(myName));
return;
}