結構體清零有什麼函數可以用麼?還有用引用來調用。。如題
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char name[20];
int math;
int eng;
int db;
} Student;
void SetZero(Student& s)
{
s.math = 0;
s.eng = 0;
s.db = 0;
}
Student* SetZero1(Student s)
{
Student *p = (Student *)malloc(sizeof(Student));
memcpy(p, &s, sizeof(Student));
p->math = 0;
p->eng = 0;
p->db = 0;
return p;
}
int main()
{
Student s;
s.math = 100;
printf("%d\n", s.math);
SetZero(s);
printf("%d\n", s.math);
s.math = 100;
printf("%d\n", s.math);
s = *SetZero1(s);
printf("%d\n", s.math);
return 0;
}
100
0
100
0