[英]SIGSEGV, Segmentation fault. while printf() value of index of an array
我想知道scanf和數組的工作原理,所以我創建了一個代碼,逐行打印數組的每個索引的值。
#include<stdio.h>
int main(){
char a[35];
scanf("%30s", a);
for(int i=0;i<30;i++){
printf("index %d value :%s\n",i,a[i]);
}
}
但是在編譯代碼時出現錯誤。 調試代碼后,出現“程序收到信號SIGSEGV,分段錯誤”。 我正在使用Dev-C ++
使用g ++進行編譯時,出現以下警告:
test.cc: In function ‘int main()’:
test.cc:6:45: warning: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘int’ [-Wformat=]
printf("index %d value :%s\n",i,a[i]);
~~~~^
您可能想將%s
更改為%c
,因為數組的每個元素都是一個字符:
printf("index %d value :%c\n",i,a[i]);
同意以上答案。 %s說取消引用arg並顯示內容。 a [i]使用8位變量作為指針。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.