我對 C 很陌生,我只是在學習陣列。這可能是最糟糕的問題,但我不明白我在這里做錯了什么:
我有一個可以存盤 10 個專案的基本陣列。當我一個一個地賦值時,我得到一個奇怪的錯誤。我看不出這里有什么問題。我看過很多教程,但我看不到自己做任何不同的事情:
#include<stdio.h>
//The Boolean lib needs to be included
#include<stdbool.h>
/**
* @file DataTypes.c
*
* Learning Data Types
*/
/**
* Primitive Data Types:
* char, int, double, float, boolan?
*/
/**
* Integers
* These could be signed or unsigned.
* The different types of Integers are:
* short, int & long.
* Difference mainly to do with memory, can learn more as I go
*/
//Basic Signed int
int number = 727;
/**
* The reason for this is because default is signed. So it could be either, so this is easy.
* This means we only need to say unsigned int when we know form sure we will not put a negative value
*
*/
int unsignedIntDidntThrowError = -33;
/**
* Explicit unsigned int.
* Use when we are sure there wont be a negative number
*/
unsigned int unsignedInt = 46;
/**
* This should throw an error because its unsigned and we gave negative value?
* Didn't throw an error. I expect this to be just a memory issue ?
*/
unsigned int unsignedIntShouldThrowError = -33;
/**
* @brief Short VS Long vs medium ?
* Just a brief about the difference, if its just memory I can leave
* There is no medium, only short & long
*/
short shortInt = 33;
long longInt = 45;
/**
* @brief Doubles & floats
* Doubles are more precise than floats
*
*/
double myFirstDouble = 636.78f;
/**
* Doubles & Floats
* Doubles & floats dont have signed / unsigned but double has a long double
*/
//Can we keep a double without trailing f? Yes!
double canIKeepDoubleVarWithoutTrailingF = 76.66;
//Float worked perfect too
float myFirstFloat = 36.78f;
/**
* Boolean?
* Lets see if boolean exists:
* Need to get the stdbool.h file, Now I realise how primitive C is,
* I could always just use 1 & 0
*/
bool boolTrueEg = true;
bool boolFalseEg = false;
/**
* Char:
* In C, is quiet primitive, strings will be done through arrays next
*
*/
char charEg = 'M';
/**
* This threw an error, probably because it is more than 1 char
*
*/
//unsigned char unsignedCharEg = '-a'; //Throws error
unsigned char unsignedCharEg = '-'; //Works
/**
* Test Arrays:
* Perfect Basic Arrays works with char, no lib needed for this
*/
char mother[] = "Mama";
/**
* Int array example:
* Unfortunately it takes curly braces which is odd, would have prefered if it was square
*/
int intArrayEg[] = {3, 6, 9};
/**
* Another int array which is initialised first so I can put the number of chars it will take
* In this example, the array can take 5 values. We test each below
* Throwing an error so we are leaving it for now
*/
int tenItemArray[10];
// tenItemArray[0] = 1;
/**
* @param argc
* @param argv
* @return int
*/
int main(int argc, char const *argv[])
{
/* code */
// printf("\nMy First Integer, var & included file with int var: %d", number);
// printf("\nMy First function: %d", getArea(9, 9));
// printf("\nResult: %f", myFirstDouble);
// printf("\nFloats work same way? %f", myFirstFloat);
// printf("\nAbstract Result: %f", canIKeepDoubleVarWithoutTrailingF);
// printf("\nShort Int is still be digit?: %d", shortInt);
// printf("\nLong Int is still be digit?: %d", shortInt);
// printf("My first Boolean, True & False: %d, %d", boolTrueEg, boolFalseEg);
// printf("Char example: %c", charEg);
// printf("Unsigned Char example: %c", unsignedCharEg);
//printf("My first char from array: %c", mother[0]);
//printf("Int Array example: %d", intArrayEg[2]);
printf("Ten Item array: %d", tenItemArray[0]);//Throws error
return 0;
}
錯誤:
PrimitiveTypes.c:118:2: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
tenItemArray[0] = 1;
^
PrimitiveTypes.c:118:2: error: redefinition of 'tenItemArray' with a different type: 'int [0]' vs 'int [10]'
PrimitiveTypes.c:117:5: note: previous definition is here
int tenItemArray[10];
^
1 warning and 1 error generated.
我似乎無法理解這些錯誤。當我剛剛使用 int 值宣告陣列時,如何缺少型別說明符?我如何重新定義陣列?我只是一個一個的加一個值
uj5u.com熱心網友回復:
我猜你有這樣的事情:
int tenItemArray[10];
tenItemArray[0] = 1;
int main() {
return 0;
}
那么這應該行不通。內移tenItemArray[0] = 1功能main。
uj5u.com熱心網友回復:
您可能不會像您所做的那樣在檔案范圍內放置陳述句。
int tenItemArray[10];
tenItemArray[0] = 1; // <== this is a statement
編譯器試圖解釋這個陳述句
tenItemArray[0] = 1; // <== this is a statement
作為宣告,并且因為作為宣告,該記錄在語法上是錯誤的,編譯器會發出顯示的錯誤訊息。
相反,您可以通過以下方式使用初始化程式宣告陣列
int tenItemArray[10] = { 1 };
或者
int tenItemArray[10] = { [0] = 1 };
在這兩種情況下,陣列的第一個元素顯式初始化為 1,所有其他元素隱式初始化為零。
uj5u.com熱心網友回復:
這被破壞的原因是因為 main() 之外的代碼是一系列宣告,并且在編譯時進行評估,而 main() 中的代碼在運行時執行。
像這樣的宣告
tenItemArray[0] = 1;
在 main 函式之外沒有意義,因為它是一個動作(陳述句)而不是一個宣告。
我的 c 有點生疏,但像這樣的東西可能允許您在編譯時初始化這些值。
int tenItemArray[10] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
編譯器錯誤有點神秘,但這是它如何看待您的代碼:
int tenItemArray[10];
// Ok, I will create tenItemArray[10] for you.
tenItemArray[0] = 1;
// Wait, this doesn't have a type. I will assume it's an int.
// Wait, you already made something called: int tenItemArray[10]
// and now you are trying to make something called: int tenItemArray[0]
// I give up.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368755.html
上一篇:在GoogleScript中使用worksheet.getrange.setvalues時出錯。我有兩個看起來相同的陣列。一個按預期作業,但另一個失敗
