我已經在一個練習中難過了幾個小時,我必須使用函數在結構中構建一個數組並打印它。在我目前的程序中,它編譯但運行時崩潰。從結構中打印陣列
#define LIM 10
typedef char letters[LIM];
typedef struct {
int counter;
letters words[LIM];
} foo;
int main(int argc, char **argv){
foo apara;
structtest(apara, LIM);
print_struct(apara);
}
int structtest(foo *p, int limit){
p->counter = 0;
int i =0;
for(i; i< limit ;i++){
strcpy(p->words[p->counter], "x");
//only filling arrays with 'x' as an example
p->counter ++;
}
return;
我確實認爲這是由於我錯誤的使用/指針組合。我試着調整它們,但產生的任何「不兼容的類型」錯誤,或數組是看似空白
}
void print_struct(foo p){
printf(p.words);
}
我還沒有做出它成功地達到了print_struct階段,但我不能確定是否p.words是要調用的正確項目。在輸出中,我希望函數返回一個x的數組。 如果我犯了某種嚴重的「我應該已經知道這個」C錯誤,我很抱歉。謝謝你的幫助。
我也不會的結構,因爲要複製的sizeof(富)堆棧傳遞給'print_struct'。請傳遞一個指針。 – Kludas