Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的C程序停止工作了_C_Structure - Fatal编程技术网

我的C程序停止工作了

我的C程序停止工作了,c,structure,C,Structure,我在这里做作业,写一个程序,显示到(0,0)一定数量点的距离。然而,由于某种原因,我的程序一启动,windows就说它已经停止工作了。我用两个不同的编译器试过,他们没有给我任何错误消息 #include <stdio.h> #include <stdlib.h> #include <math.h> struct point { int x; int y; }; struct point getPoint(); void printPoint

我在这里做作业,写一个程序,显示到(0,0)一定数量点的距离。然而,由于某种原因,我的程序一启动,windows就说它已经停止工作了。我用两个不同的编译器试过,他们没有给我任何错误消息

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct point {
    int x;
    int y;
};

struct point getPoint();
void printPoint(struct point);
double distanceToO(struct point p);
void createArray(struct point, int);

int main() {

    int number, i;
    struct point coord[number];

    printf("Type the number of points you want to create: ");
    scanf("%d", &number);

    printf("\n\n");

    for(i=0;i<number;i++)
        coord[i]=getPoint();

    printf("\n\t\tPoint\tDistance to (0,0)\n");

    for(i=0;i<number;i++) {        
        printPoint(coord[i]);

        printf("\t%0.2lf", distanceToO(coord[i]));
    }

    system("pause"); 
    return 0;
}

struct point getPoint() {
    struct point p;

    printf("Type the x and the y-value for a point with a space in between: ");
    scanf("%d %d", &p.x, &p.y);

    return p;   
}

void printPoint(struct point p){
    printf("\n\t\t(%d,%d)",p.x,p.y);
}

double distanceToO(struct point p) {
    return sqrt((0-p.x)*(0-p.x)+(0-p.y)*(0-p.y));
}
#包括
#包括
#包括
结构点{
int x;
int-y;
};
结构点getPoint();
无效打印点(结构点);
双距离(结构点p);
void createArray(结构点,int);
int main(){
整数,i;
结构点坐标[编号];
printf(“键入要创建的点数:”;
scanf(“%d”和编号);
printf(“\n\n”);

对于(i=0;i变量
number
在用于指定数组
coord
中的元素数时未初始化。然后访问数组,不知道数组中有多少元素。在使用之前,将有效值读入
number
,并检查是否确实读取了有效值:

/* scanf() returns number of assignments made. */
if (scanf("%d", &number) == 1)
{
}
始终检查输入操作的结果,以确保后续代码正在处理具有有效值的变量

int number, i;
struct point coord[number];
number
尚未初始化,您正在使用它声明
coord
数组的大小。您将在堆栈上生成一个有效随机大小的数组,这可能会导致崩溃

int number;
struct point coord[number];
我看不到在哪里初始化了
number
。如果您真的想使用VLA,您应该在之后声明
coord

int number;

scanf("%d", &number);

struct point coord[number];

否则,由于
number
具有自动存储持续时间,因此其值将未定义。

我很惊讶您没有收到以下警告/错误:

int number, i;
struct point coord[number];
分配一个大小等于单位化变量的
结构点数组

请注意,如果使用Visual Studio,它也不完全支持C99标准,因此不允许在声明之前使用语句,如:

int number;
number = some_number;
struct point coord; // Error, you have a statement above

你试过调试它吗?我注意到的第一件事是这一行:
struct point coord[number];
你试图使用一个尚未初始化的数字创建一个数组。你应该在编译器上启用警告,例如使用gcc:-Wall-wextra,尽管这需要C99+。@Jite:因为OP已经使用了VLA(C99)在他的原始代码中,我想他知道这一点。我所知道的是MS实现了它的一些功能,但不是全部。声明之前的语句是他们无论如何都不支持的(反之亦然,声明之后的语句).你可能是对的,但是,其他人必须填写这一部分。我更新了示例,使其不包含VLA,因为示例不需要它。