我正在学习 C 中的函数,并且遇到以下问题并出现很多警告:-(
代码如下
#include <stdio.h>
void main(){
int a,c;
char *b; // declaring a pointer
char string[100]="something";
b = string; // assigning a pointer.. doing
printf("\n %s \n\n %s",string,*b); // doing this as a verification of the pointer, which is good - no seg faults
printf("Enter a and c:-");
scanf("%d %d",&a,&c);
find(a,c,*b);
printf("%s",*b);//segmentation fault core dumped:-'(
}
void find(int x, int y,char *b){
if(x>y)
*b = "a is greater than b";
else if(x=y)
*b = "both the values are equal";
else
*b = "b is greater than a";
}
编译时出现警告:--
function.c: In function ‘main’:
function.c:7:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
printf("\n %s \n\n %s",string,*b); /*doing this jus to check is pointer working but no it is *not.segmentation error here "core dumped":-'(
^
function.c:12:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s",*b);//segmentation fault core dumped:-'(
^
function.c: At top level:
function.c:14:6: warning: conflicting types for ‘find’ [enabled by default]
void find(int x, int y,char *b){
^
function.c:11:2: note: previous implicit declaration of ‘find’ was here
find(a,c,*b);
^
function.c: In function ‘find’:
function.c:16:6: warning: assignment makes integer from pointer without a cast [enabled by default]
*b = "a is greater than b";
^
function.c:18:6: warning: assignment makes integer from pointer without a cast [enabled by default]
*b = "both the values are equal";
^
function.c:20:6: warning: assignment makes integer from pointer without a cast [enabled by default]
*b = "b is greater than a";
运行时
<小时/>- 段错误(核心转储)
工作 cod:=---- 在社区的帮助下
#include <stdio.h>
#include <malloc.h>
void main(){
int a,c;
char *b =(char *)malloc(100);
char string[100]="something";
b = &string;
printf("\n %s \n\n %s",string,b);
printf("Enter a and c:-");
scanf("%d %d",&a,&c);
find(a,c,*b);
printf("\n%s",b);
}
void find(int x, int y,char *b){
if(x>y)
b = "a is greater than b";
else if(x=y)
b = "both the values are equal";
else
b = "b is greater than a";
}
输出:- 某事
某事 输入 a 和 c:- 10
20
某事
**
- 意味着它仍然没有更新函数中的值...
**
最佳答案
我在不到一分钟的时间内修复了你的代码。没有警告并且可能有效(我不是在谈论逻辑问题)。这意味着你的警告是即使是经验丰富的工程师也可能会犯的常见错误。但是聪明的工程师会使用警告来修复这些警告。
#include <stdio.h>
#include <malloc.h>
void find(int, int,char*);
int main(){
int a,c;
char *b = NULL;
char string[100]="something";
b = &string[0];
printf("\n %s \n\n %s",string,b);
printf("Enter a and c:-");
scanf("%d %d",&a,&c);
find(a,c,b);
printf("\n%s",b);
return 0;
}
void find(int x, int y,char *b){
if(x>y)
b = "a is greater than b";
else if(x == y)
b = "both the values are equal";
else
b = "b is greater than a";
}
如果你检查这个,你会发现现在没有警告。 关于您的学习努力,我会尝试解释警告并尝试估计并向您提供这些警告将导致的结果的总体情况。
warning: return type of ‘main’ is not ‘int’
-这里您可以找到为什么我们更喜欢在 main 中返回 int 的答案。 int main vs void main
warning: suggest parentheses around assignment used as truth value
-这是你遇到的最严重的错误。 = 用于分配,== 用于比较 Difference between = and ==
其他也有错误,
conflicting type
和
implicit declaration
意味着编译器的预处理器在函数声明之前到达了函数调用( find(a,c,*b); )。因此,一个编译器可能会修复该问题并自动解决此问题,而另一个编译器可能会出现错误。这就是为什么预处理器首先检查头文件,但由于你没有头文件(这很糟糕),所以在调用它之前你应该有一个函数的声明。 当您修复时,您会收到警告,
warning: passing argument 3 of ‘find’ makes pointer from integer without a cast
这意味着您正在尝试传递指针的指针。 b 是第一个位置的指针,它保存 char 数组的第一个元素的地址。所以 y 应该在不带 * 的情况下传递它。
这是关于为什么我们不忽略错误这个问题的一个很好的例子
.由于有关函数隐式声明的错误,导致发现隐藏在第一个警告下的另一个错误,该错误比第一个错误更严重。
另一个注意事项是,您不需要在那里进行 malloc。当您想要在堆上分配一定量的内存并在释放它之前使其保持事件状态时,可以进行 malloc。
PS:我希望我能有所帮助,也希望您能从中受益。如果您有任何问题,请随时发表评论(最好在聊天中,以避免社区中出现垃圾邮件)
谢谢。
关于c - "segmentation error (core dumped)"使用函数和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503754/