【发布时间】:2020-01-08 20:49:48
【问题描述】:
那是我的 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;
}```
【问题讨论】:
-
您的数组大小为零。它们不会追溯调整大小。