Re: [問題]傳遞結構位址

看板C_and_CPP (C/C++)作者 (真的不強求....)時間16年前 (2010/05/15 23:30), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串2/2 (看更多)
這是課本交換的範例 #include <stdio.h> #include <stdlib.h> struct student { int id; char name[10]; }; void sumfunc(struct student *,struct student *); int main(int argc, char *argv[]) { struct student stu={5,"okok"}; struct student stu1={6,"koko"}; sumfunc(&stu,&stu1); printf("%d",stu.id); printf("%d",stu1.id); system("PAUSE"); return 0; } void sumfunc(struct student *ptr,struct student *ptr1) { struct student tmp; tmp=*ptr; *ptr=*ptr1; *ptr1=tmp; } 為什麼可以這樣做 用*ptr 操作 這是我的疑點 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 120.126.10.54

05/16 00:00, , 1F
這樣想吧: tmp.id = (*ptr).id;
05/16 00:00, 1F

05/16 00:35, , 2F
你應該要去了解指標到底在幹嘛,不然你不知道問題錯在哪!
05/16 00:35, 2F

05/16 10:46, , 3F
感謝大家的回答~
05/16 10:46, 3F
文章代碼(AID): #1BxhueNj (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1BxhueNj (C_and_CPP)