[問題] 指標中修改char陣列的某個值

看板C_and_CPP作者 (早起精神好)時間10年前 (2013/11/26 21:45), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Linux 問題(Question): 最近在學習指標 想要寫一個struct含char[100] 運用Linked list使其能做增加node減少node和修改某個node的某個Byte 其中修改某個node的特定byte的值 不知該如何去做 程式碼(Code):(請善用置底文網頁, 記得排版) struct node { int PageNumber; char name[MAX]; struct node *next; }; void edit() { int byte,page; printf("edit which page?\n"); scanf("%d",&page); struct node *s; int key; s= first; while( s!=NULL) { if(page == s->PageNumber) break; else { pred =s ; s = s-> next; } } current =s; if (current!=NULL) printf("%d\n",s->PageNumber); printf("which byte?"); scanf("%d",&byte); printf("%d\n",s->PageNumber); *(&(s->name)+byte)= 'R'; } 其中已經確定能得到正確的pagenumber 但是要如何改這個page(node)中的特定Byte呢? *(&(s->name)+byte)= 'R'; 請問這個方法不對嗎? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.166.121.94

11/26 21:58, , 1F
current->name[byte] = 'R';
11/26 21:58, 1F
※ 編輯: tanker 來自: 118.166.121.94 (11/26 22:16) 另外在Linux上complie有時發生segmentation fault 如 char *s="1"; int a; a = atoi (s); printf("%d",a); 這樣編譯會過 但執行到這裡就會中斷產生segmentation fault 請問是甚麼問題呢? ※ 編輯: tanker 來自: 118.166.121.94 (11/26 22:22)

11/26 22:34, , 2F
你知道a 和 &a 差異嗎? 這裡印數字用 a
11/26 22:34, 2F
抱歉 寫太快 ※ 編輯: tanker 來自: 118.166.121.94 (11/26 22:38)

11/26 22:48, , 3F
你可能偷改到s,加const測看看。
11/26 22:48, 3F
不好意思 問個簡單的問題 char *s; scanf("%s",s); int a; a = atoi(s); 這樣會build不過 是因為沒設初值的關係嗎? 若改成 char s[10]; 這樣就可以過 這邊的觀念有點不太了解 ※ 編輯: tanker 來自: 118.166.120.112 (11/27 22:17)

11/27 22:27, , 4F
置底第三誡
11/27 22:27, 4F
謝謝 ※ 編輯: tanker 來自: 118.166.120.112 (11/27 22:43)
文章代碼(AID): #1IbANvDk (C_and_CPP)