Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 数组没有在循环中存储值_C_Arrays_For Loop - Fatal编程技术网

C 数组没有在循环中存储值

C 数组没有在循环中存储值,c,arrays,for-loop,C,Arrays,For Loop,这是我的C代码。我试图使用for循环在数组中存储值,但没有存储任何内容,只有变量trackingCodes的值发生更改。我不知道错误来自哪里。没有编译错误 #include <stdio.h> int main(void) { int trackingCodes = 0; char typeCode[trackingCodes]; int codeLength [trackingCodes]; int byteChar = 0; int byteInt = 0; int byteD

这是我的C代码。我试图使用for循环在数组中存储值,但没有存储任何内容,只有变量
trackingCodes
的值发生更改。我不知道错误来自哪里。没有编译错误

#include <stdio.h>

int main(void) {
int trackingCodes = 0;
char typeCode[trackingCodes];
int codeLength [trackingCodes];
int byteChar = 0;
int byteInt = 0;
int byteDouble = 0;
int j = 0;
int totalBytes = 0;
int totalByteDouble = 0;
int totalByteInt = 0;
int totalByteChar = 0;


scanf("%d", &trackingCodes);

for ( j = 0; j < trackingCodes; j++)
{
    scanf("%d %c", &codeLength[j], &typeCode[j]);
    if (typeCode[j] == 'c')
    {
        byteChar = codeLength[j] * sizeof(char);
        totalByteChar = totalByteChar + byteChar;

    }
     else if (typeCode[j] == 'i')
    {
        byteInt = codeLength[j] * sizeof(int);
        totalByteInt = totalByteInt + byteInt;

    }
     else if (typeCode[j] == 'd')
    {
        byteDouble = codeLength[j] * sizeof(double);
        totalByteDouble = totalByteDouble + byteDouble;

    } 

}

    totalBytes = totalByteChar + totalByteDouble + totalByteInt;
    int t = 0;

    for(t = 0; t < trackingCodes; t++){
        if(codeLength[t] != 'i' && codeLength[t] != 'c' && codeLength[t] != 'd'){
            printf("Invalid Tracking code type");
            return 0;
        }
    }

    printf("%d bytes\n", totalBytes);



    return 0;
}```
#包括
内部主(空){
int trackingcode=0;
字符类型代码[跟踪代码];
int代码长度[跟踪代码];
int-byteChar=0;
int byteInt=0;
int byteDouble=0;
int j=0;
int totalBytes=0;
int totalByteDouble=0;
int totalByteInt=0;
int totalByteChar=0;
scanf(“%d”和跟踪代码);
对于(j=0;j
您需要移动数组定义,以便在读取了
跟踪代码之后移动数组定义。通过这种方式,您可以创建一个具有扫描大小的可变长度数组。比如:

scanf("%d", &trackingCodes);

char typeCode[trackingCodes];
int codeLength [trackingCodes];
而且。。。始终检查scanf的返回值,即:

if (scanf("%d", &trackingCodes) != 1)
{
    // error handling
}

您需要移动数组定义,以便在读取
trackingCodes
后移动数组定义。通过这种方式,您可以创建一个具有扫描大小的可变长度数组。比如:

scanf("%d", &trackingCodes);

char typeCode[trackingCodes];
int codeLength [trackingCodes];
而且。。。始终检查scanf的返回值,即:

if (scanf("%d", &trackingCodes) != 1)
{
    // error handling
}

我认为您认为代码:

int trackingCodes = 0;
char typeCode[trackingCode];
将无限期地将
typeCode
的大小绑定到
trackingCodes
的值。它实际上将用当前值的大小
trackingCodes
声明
typeCode


解决方案是从@4386427的答案中所述的
stdin
扫描您的输入,或者使用动态内存管理(
malloc
/
free
和co.)分配它。

我想您认为代码:

int trackingCodes = 0;
char typeCode[trackingCode];
将无限期地将
typeCode
的大小绑定到
trackingCodes
的值。它实际上将用当前值的大小
trackingCodes
声明
typeCode


解决方案是从@4386427的答案中所述的
stdin
扫描您的输入,或者使用动态内存管理(
malloc
/
free
和co.)分配它。

您的阵列大小为零。它们的大小不能追溯。数组的大小为零。它们的大小不能追溯。