C编程中转储的核心 [英] core dumped in C programming

查看:105
本文介绍了C编程中转储的核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个家庭作业,应该向用户询问一个数字,然后做一个正方形,长度就是他们输入的数字。如果用户输入5,则程序需要制作一个5x5的正方形。

I have a homework that's supposed to ask the user for a number and make a square with the length being the number they put. If the user types in 5, then the program needs to make a square that is 5x5.

当我编译它时,我遇到了一个分段错误(核心已转储)。我不知道问题出在哪里。你们能帮我吗?我什至尝试用纸和铅笔运行该程序,以查看输出结果,对我来说似乎很好。

When I compile it, i get a segmentation fault (core dumped). I have no idea where the problem is. Can you guys help me? I even tried running the program with paper and pencil to see what my output would be and it seemed fine to me.

#include <stdio.h>

int main (){

int size, limit = 0;
char ch = 'A';
int rows = 1;

printf("Enter size:\n");
scanf("%d", size);

while (limit <= size){

    if (rows == 1 || rows == size){  /* This only works in first and last row */
       printf("%c", ch);
       limit++;
    }

    if (rows != 1 && rows != size){ /* This only works if row is not 1 and last row*/
       do{
            printf("%c", ch);
            limit++;

            do {
               printf(" ");
               limit++;
            } while (limit != size -1);

            limit++;

            if (limit == size){
               printf("%c", ch);
               rows++;
               limit = 0;
               printf("\n");
            }

       }while (rows != 1 && rows != size);  /* while not in first AND last row */

    }


推荐答案

int size, limit = 0;

...

scanf("%d", size);

启用编译警告,这是错误的。

Enable your compile warnings, this is wrong.

这篇关于C编程中转储的核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆