c - 我的 C 程序停止工作

标签 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(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));
}

具体操作如下:

编写一个程序,首先询问应该创建多少个点,然后询问用户这些点的 x 和 y 值。 然后程序应该给出一个表格,显示点和到 (0,0) 的距离。 必须创建/使用以下函数: "point getpoint()"- 要求输入坐标 "void printpoint(point p)"- 打印点的坐标 "double distanceToO(point p)"- 返回到 (0,0) 的距离 创建一个结构点,它有两个成员点的 x 坐标和 y 坐标。

谁能告诉我哪里出了问题?

最佳答案

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

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

关于c - 我的 C 程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15248926/

相关文章:

c++ - C/C++ 有哪些替代语法? (想想 SPECS 或 Mirah)

c - 无效的转义序列在 C 中如何表现?

有人可以使用可视化图表解释 C 中 **param 和 *param 之间的区别吗?

c - 平衡 Linux 内核中的内存使用

c++ - 我可以让 C++ 代码结构相互看到吗?

jsf - 使用 JSF 绘制图形结构

c - 使用 C 预处理器宏构建字符串

linux - 使用相同的文件夹结构移动早于特定时间的文件和目录

c++ - 实现二维范围树 C++

ruby -/lib 中的 <appname>.rb 文件在 Ruby 项目中通常用于什么?