我遇到了struct數組的問題。 我試圖將一個字符串的一部分複製到一個結構數組的一個元素。 (抱歉,如果這聽起來並不清楚)如何將一部分字符串複製到struct數組的元素中?
這裏是我的代碼
#include <stdio.h>
#include <string.h>
struct dict {
char key[1024];
char value[16384];
int level;
};
int main()
{
struct dict entry[2562];
char str[]="i will finish my mp";
int j=0;
int i = 0;
char temp[1024];
char a =0;
while(a != 'h'){
a = str[i];
temp[i] = str[i];
i++;
}
strcpy(entry[0].value,str);
puts(entry[0].value);
return 0;
}
它編譯但它確實分段錯誤,我不知道是什麼地方錯了 請幫助
'struct dict entry [2562];'可能有點太大而不適合自動存儲(「在堆棧上」)使其成爲全局或動態分配。 – wildplasser 2012-03-07 12:12:59
由於您在堆棧中分配了大型數組'struct dict entry [2562]',因此可能會出現段錯誤。嘗試將其移出該功能並查看它是否有效。 – dasblinkenlight 2012-03-07 12:13:39
嘗試在調試器中運行它,它將幫助您查明錯誤位置並讓您檢查變量以查看可能存在的問題。 – 2012-03-07 12:16:16