【问题标题】:Error in code, programming in C代码错误,用C编程
【发布时间】: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


【解决方案1】:

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

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
    }
}

【讨论】:

  • 谢谢!我要破译这个,看看我得到了什么
【解决方案2】:

您不能通过传递变量(数量)作为数组大小来静态分配数组。为此,您需要使用malloc 动态分配它。
此外,数组中的索引是从 0 开始的。这意味着您可以访问从 0 到 size-1 的元素。如果你通过 myvector[size] 你尝试在最后一个数组元素之后读取。

【讨论】:

  • 我对编写代码很陌生,这意味着我对这种技术语言很陌生。如果我理解正确,您是说我不能通过使用变量来声明大小来创建一定大小的数组??
  • 你不能使用变量,因为当你静态分配一个数组时,它的大小必须在编译时确定。这意味着当您构建可执行文件时,编译器会为其保留特定数量的内存。由于在您的程序中,数组大小将在运行时确定,因此您需要动态分配它。
猜你喜欢
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
相关资源
最近更新 更多