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

标签 c codeblocks scanf

我正在编写一个小程序作为练习:它假设添加 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/

相关文章:

c - scanf() 无法正常工作?

c - 在结构中使用指针

c - switch 和 if else 语句的区别

c - 关于将 CLI 解析器链接到我的应用程序

c - scanf 内存分配错误

c - 在带有 gcc 的 sscanf 中使用 '-' 字符

c++ - 矢量化是什么意思?

c++ - Code::Blocks pthread 示例无法编译

c - mariadb c api windows undefined reference to

c++ - 无效的预处理指令 mingw