;//如果結構體中有指標 在堆上開闢的空間
//以下這個拷貝函式, 就是淺拷貝
//結構體可以通過變數直接賦值, 但不要使用這種方法,
//要給結構體中的成員 乙個乙個拷貝
void copy_teacher(struct teacher *to, struct teacher *from)
void copy_teacher_deep(struct teacher *to, struct teacher *from)
void print_teacher(struct teacher *tp)
int main(void)
; //在棧上開闢的乙個teacher結構體
struct teacher tp2 = ;
tp1.id = 1;
tp1.name = (char*)malloc(name_len);
memset(tp1.name, 0, name_len);
strcpy(tp1.name, "zhang3");
//copy_teacher(&tp2, &tp1);
copy_teacher_deep(&tp2, &tp1);
print_teacher(&tp1);
print_teacher(&tp2);
if (tp1.name != null)
if (tp2.name != null)
return 0;
}
結構體的深拷貝和淺拷貝
示例 define crt secure no warnings include include include typedef struct teacher teacher 淺拷貝 編譯器的操作只會把指標變數的值,從from copy到 to,但不會把指標變數所指的記憶體空間給拷貝過去 void ...
C 的淺拷貝和深拷貝(結構體)
關於淺拷貝和深拷貝這個問題遇上的次數不多,這次遇上整理一下,先說這樣乙個問題,關於淺拷貝的問題,先從最簡單的說起。假設存在乙個結構體 struct student int main struct student stu2 stu2 stu cout age is 18 name is liming ...
深拷貝和淺拷貝問題
下午遇到乙個關於乙個表的數值拷貝的問題,有點意思,不多說,直接上 記錄玩家的資訊 self.userinfo for i 1,play count do table.insert self.userinfo,self.deskuserlist getuserbydeskstation i 1 end...