什么是分段故障(核心转储)? [英] what is Segmentation fault (core dumped)?

查看:588
本文介绍了什么是分段故障(核心转储)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写在linux下一个C程序有争论的开方,这里的code:

 #包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&MATH.H GT;INT主(字符* argv的[]){
    浮K表;
    的printf(这是消费者\\ n);
    K =(浮动)的sqrt(与atoi(ARGV [1]));
    的printf(%S \\ n,K);
    返回0;
}

在我在输入端输入壳>提示符下,海湾合作委员会给了我以下错误:

 分割故障(核心转储)


解决方案

段错误是指您尝试访问您没有访问内存。

第一个问题是你的的主要参数。在功能应该是 INT主(INT ARGC,CHAR *的argv []),你应该检查 ARGC 至少2访问之前的argv [1]

此外,由于你传递一个浮动的printf (其中,顺便说一句,被转换到双传递给的printf ),你应该使用%F C $ C>格式说明。在%S 格式说明是字符串('\\ 0'封端的字符数组)。

I am trying to write a C program in linux that having sqrt of the argument, Here's the code:

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

int main(char *argv[]){
    float k;
    printf("this is consumer\n");
    k=(float)sqrt(atoi(argv[1]));
    printf("%s\n",k);
    return 0;
}

After I type in my input at the "shell> " prompt, gcc gives me the following error:

Segmentation fault (core dumped)

解决方案

"Segmentation fault" means that you tried to access memory that you do not have access to.

The first problem is with your arguments of main. The main function should be int main(int argc, char *argv[]), and you should check that argc is at least 2 before accessing argv[1].

Also, since you're passing in a float to printf (which, by the way, gets converted to a double when passing to printf), you should use the %f format specifier. The %s format specifier is for strings ('\0'-terminated character arrays).

这篇关于什么是分段故障(核心转储)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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