gpt4 book ai didi

c - 代码错误,用C语言编程

转载 作者:行者123 更新时间:2023-11-30 21:40:18 25 4
gpt4 key购买 nike

我正在编写一个小程序作为练习:它假设添加 vector 。我从 IDE 收到一条错误,指出:“错误:下标值既不是数组,也不是指针,也不是 vector 。”当我执行 for 循环并让用户使用 scanf() 函数输入一些 float 类型的数据时,就会发生这种情况。我将在下面发布代码,以便您自己查看。

#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


}



}

最佳答案

您需要使用 malloc 分配一个 vector 数组。

typedef struct
{
float x;
float y;
}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("Count: ");
scanf("%d", &amount);
if(amount<1) { printf("error, need vector size %d > 0\n",amount); exit(1); }
vectors *vectarray = malloc(sizeof(vectors)*amount); //allocate the vector for the given vector size
if(!vectarray) { printf("error, cannot allocate vectorarray[%d]\n",amount); exit(2); }
initializeVector(vectarray,amount); //<-- This is a function call.
return 0;
}

然后您需要将该数组(作为指针)传递给初始值设定项,并且需要提供 x,y 组件的地址,而不仅仅是组件的地址。

vectors initializeVector(vectors userVect[], int length) //pass an array
{
/* 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].x)); //scanf wants a pointer to the x component
printf("\nY: ");
scanf("%f", &(userVect[i].y)); //scanf wants a pointer to the y component
}
}

关于c - 代码错误,用C语言编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20694307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com