简体   繁体   English

错误:在 function 中打印数组时,下标值既不是数组也不是指针也不是向量(C)

[英]error: subscripted value is neither array nor pointer nor vector when printing an array within a function (C)

Hi there I'm trying to print an array but I keep getting this error out (error: subscripted value is neither array nor pointer nor vector) whenever I try and print from a function.嗨,我正在尝试打印一个数组,但每当我尝试从 function 打印时,我都会不断出现此错误(错误:下标值既不是数组也不是指针也不是向量)。 As its a time I want only the data within the array not the position of the data as well.作为一个时间,我只想要数组中的数据而不是数据的 position。
***It also works if its not in a function. ***如果它不在 function 中,它也可以工作。

My code is as follows:我的代码如下:

int currenttime[] = {00,00,00};
int i;
void outputtime(int currenttime)
{   
    for(i = 0; i<3; i++) 
    {
     printf("%d ", i, currenttime[i]);
    }
}

int main()
{
outputtime(currenttime);
return 0;
}

You need to provide parameters to match the prototype of a called function.你需要提供参数来匹配一个叫function的原型。
Either you keep the prototype and call accordingly, or you adapt the prototype.要么保留原型并相应地调用,要么调整原型。

This is giving a pointer to int (to which the use of an array identifier in a function call decays) to a function which you declared to expect an int.这是一个指向 int 的指针(在 function 调用中使用数组标识符衰减)到您声明期望一个 int 的 function。

outputtime(currenttime);

From the context I conclude that in this case your call is actually what you want to do, so the prototype needs to be adapted to declare a parameter of pointer to int .从上下文中我得出结论,在这种情况下,您的调用实际上就是您想要做的,因此需要调整原型以声明指向int的指针参数。

void outputtime(int* currenttime)

But that gives an output of "1 2 3".但这给出了“1 2 3”的 output。
This happens because in your call to printf() you provide two values ( i and currenttime[i] ), with only one being used by the format specifier "%d " .发生这种情况是因为在您调用printf()时,您提供了两个值( icurrenttime[i] ),格式说明符"%d "只使用了一个值。

Either you want to print the two values and need to adapt the specifier您想要打印这两个值并需要调整说明符

printf("%d:%d, ", i, currenttime[i]);

which gets you an output of这会给你一个 output

0:0, 1:0, 2:0, 

or you remove the first value to only output the content of the array.或者您将第一个值删除为仅 output 数组的内容。

printf("%d ", currenttime[i]);

for an output of对于 output 的

0 0 0 

In case you now wonder why your init of the array of如果您现在想知道为什么您的数组的 init

int currenttime[] = {00,00,00};

only gets you single 0s for each part;每个部分只会给你一个 0; that is because the 00 are identical to 0 .那是因为000相同。 If you want to change how a 0 is printed, you need to use other features of printf() , after reading the details in the specification.如果要更改 0 的打印方式,则需要在阅读规范中的详细信息后使用printf()的其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:C中的下标值既不是数组也不是指针也不是矢量 - error: subscripted value is neither array nor pointer nor vector in C 错误 - C 中的下标值既不是数组也不是指针也不是向量 - Error - Subscripted value is neither array nor pointer nor vector in C 下标的值既不是数组,也不是指针,也不是向量 - subscripted value is neither array nor pointer nor vector in c C程序中的下标值既不是数组也不是指针也不是向量 - Subscripted value is neither array nor pointer nor vector in C program C下标值既不是数组也不是指针也不是向量 - C subscripted value is neither array nor pointer nor vector C-下标值既不是数组,也不是指针,也不是向量 - C - subscripted value is neither array nor pointer nor vector 下标值既不是数组也不是指针也不是向量的错误 - Error with subscripted value being neither array nor pointer nor vector “错误:下标值既不是数组,也不是指针,也不是矢量”,但是为什么呢? - “error: subscripted value is neither array nor pointer nor vector”, but why? C 赋值数组元素值时下标值既不是数组也不是指针也不是向量 - C subscripted value is neither array nor pointer nor vector when assigning an array element value 下标值既不是数组也不是指针,也不是数组索引处的向量 - Subscripted value is neither array nor pointer nor vector at array index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM