我有一個學校專案,我必須在 .h 頭檔案中制作我的結構和函式。
我已經創建了我的結構,但不能使用其中的任何變數,因為每當我呼叫它時,它都會突出顯示結構名稱并告訴我它未定義,即使它顯然在我的結構中并且沒有突出顯示或給我任何語法錯誤.
#include <stdio.h>
typedef struct test1 {
int array1[3];
int array2[3];
};
int main(void) {
scanf_s(" %d %d", &test1.array1[1], &test1.array2[1]);
}
我嘗試過使用 typedef 和不使用它,結果相同。如果我在結構之外創建單個變數,我不會遇到任何問題,所以我相信我創建結構的方式存在一些問題,但我不知道問題是什么。
uj5u.com熱心網友回復:
使用typedef使我認為您實際上想要定義一個名為 的型別test1。將名稱移動到struct:
#include <stdio.h>
typedef struct {
int array1[3];
int array2[3];
} test1; // now a name you can use
然后,您需要創建一個實體,test1以便能夠將其用于scanf_s:
int main(void) {
test1 t1; // `t1` is now a `test1` instance
scanf_s(" %d %d", &t1.array1[1], &t1.array2[1]);
// ^^ ^^
}
uj5u.com熱心網友回復:
您宣告struct test1了型別說明符(而且 typedef 宣告甚至沒有為型別說明符宣告 typedef 名稱struct test1)
typedef struct test1 {
int array1[3];
int array2[3];
};
但是的呼喚scanf_s
scanf_s(" %d %d", &test1.array1[1], &test1.array2[1]);
期望物件。test1不是一個物件。這個名字甚至沒有宣布。
你可以寫例如
struct test1 test1;
scanf_s(" %d %d", &test1.array1[1], &test1.array2[1]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/534182.html
標籤:C目的结构宣言类型定义
