[問題]傳遞結構位址

看板C_and_CPP (C/C++)作者 (真的不強求....)時間16年前 (2010/05/15 23:03), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串1/2 (看更多)
#include <stdio.h> #include <stdlib.h> struct student { int id; char name[10]; }; void sumfunc(struct student *); int main(int argc, char *argv[]) { struct student stu={5,"okok"}; printf("%d\n",stu.id); sumfunc(&stu); printf("%d\n",stu.id); //puts(++ptr); system("PAUSE"); return 0; } void sumfunc(struct student *ptr) { *ptr=10; // <<我的問題點 } 想利用函數將ID值改為10 我的問題點這樣寫為什麼不能編譯 修改成 ptr->id=10; ptr+1->name="koko"; <<則此行不能編譯 感謝大家的回答 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 120.126.10.54

05/15 23:09, , 1F
struct不是array, 要這樣 strcpy(ptr->name, "koko");
05/15 23:09, 1F

05/16 00:00, , 2F
只想提一下ptr+1表示位址增加1個sizeof(struct student)
05/16 00:00, 2F

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