C 中最基本的一维数组无法按预期工作

Most Basic 1D Array in C not working as expected

我是 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 值声明数组时,类型说明符怎么会丢失? 我如何重新定义数组?我只是一个一个地加一个值

我猜你有这样的事情:

int tenItemArray[10];
tenItemArray[0] = 1;


int main() {

    return 0;
}

那么这应该行不通。在函数 main.

内移动 tenItemArray[0] = 1

您不能像以前那样在文件范围内放置语句。

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,所有其他元素隐式初始化为零。

这被破坏的原因是因为 main() 之外的代码是一系列声明并在编译时评估,而 main() 中的代码在 运行 时执行。

这样的说法

tenItemArray[0] = 1;

在主函数之外没有意义,因为它是一个动作(语句)而不是声明。

我的 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.