鱼C论坛

 找回密码
 立即注册
查看: 1918|回复: 6

[已解决]运行后出现segmentation fault,有大佬能帮看一下吗

[复制链接]
发表于 2020-10-12 20:31:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
int n;
int i;
int *square=NULL;
printf("Enter the side of a square here: \n");
scanf("%d",n);
square=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
最佳答案
2020-10-12 21:19:34
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void main(void)
  4. {
  5. int n;
  6. int i;
  7. int *square=NULL;
  8. printf("Enter the side of a square here: \n");
  9. scanf("%d",&n); // 这里少了个&
  10. square=(int *)malloc(n*sizeof(int));
  11. for(i=0;i<n;i++)
  12. {
  13.         square=n;
  14. }
  15. for(i=0;i<n;i++)
  16. {
  17.         printf("%d",square);
  18. }
  19. free(square);
  20. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-12 20:35:00 | 显示全部楼层
代码发全
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-12 21:06:40 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
int n;
int i;
int *square=NULL;
printf("Enter the side of a square here: \n");
scanf("%d",n);
square=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
        square[i]=n;
}
for(i=0;i<n;i++)
{
        printf("%d",square[i]);
}
free(square);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-12 21:07:15 | 显示全部楼层

#include<stdio.h>
#include<stdlib.h>
void main(void)
{
int n;
int i;
int *square=NULL;
printf("Enter the side of a square here: \n");
scanf("%d",n);
square=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
        square=n;
}
for(i=0;i<n;i++)
{
        printf("%d",square);
}
free(square);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-12 21:19:34 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void main(void)
  4. {
  5. int n;
  6. int i;
  7. int *square=NULL;
  8. printf("Enter the side of a square here: \n");
  9. scanf("%d",&n); // 这里少了个&
  10. square=(int *)malloc(n*sizeof(int));
  11. for(i=0;i<n;i++)
  12. {
  13.         square=n;
  14. }
  15. for(i=0;i<n;i++)
  16. {
  17.         printf("%d",square);
  18. }
  19. free(square);
  20. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-12 21:36:09 | 显示全部楼层

谢谢谢谢!初学代码还是有点粗心!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-12 21:40:18 | 显示全部楼层
空羊羊 发表于 2020-10-12 21:36
谢谢谢谢!初学代码还是有点粗心!

嗯,初学者经常犯这种错误
当年我找了一个多小时,还是没找到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 23:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表