【发布时间】:2013-12-20 00:27:47
【问题描述】:
我正在编写一个小程序作为练习:它假设添加向量。 我从 IDE 收到一条错误消息,指出:“错误:下标值既不是数组也不是指针也不是向量。” 当我通过一个 for 循环并让用户使用 scanf() 函数输入一些浮点类型的数据时,就会发生这种情况。我将在下面发布代码,以便您自己查看。
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
float xcomp;
float ycomp;
}vectors;
vectors initializeVector(vectors userVect, int length);
int main()
{
/* This block determines the amount of vectors that will be added.
It will then create an array of vector structs the size of the amount being added.
Afterwards it will call a function that will grab the users data (vector information) and use
to add the vectors in the array.*/
int amount;
printf("How many vectors would you like to add?\n");
printf("AMOUNT: ");
scanf("%d", &amount);
vectors myvect[amount];
initializeVector(myvect[amount],amount); //<-- This is a function call.
return 0;
}
vectors initializeVector(vectors userVect, int length)
{
/* This function will go through the array of vectors and allow the user
to input the components of each vector in the array.*/
printf("Enter the 'X' and 'Y' components of your vector\n");
int i;
for(i=0; i<length;i++)
{
printf("%d", i+1);
printf(" vector:\n");
printf("X: ");
scanf("%f", userVect[i].xcomp); // This line brings up the error
printf("\nY: ");
scanf("%f", userVect[i].ycomp); // this line brings up the error
}
}
【问题讨论】:
-
那么为什么要标记c++?重新标记...
-
向量 *userVect; userVect[i]->xcomp;
-
因为“userVect”参数不是数组。你不能索引它。
-
你的代码格式很丑!
-
-1 for:未能完全描述问题,未能描述您尝试解决的问题以及可怕的问题标题
标签: c codeblocks scanf