无法理解此错误

编程入门 行业动态 更新时间:2024-10-24 11:19:41
本文介绍了无法理解此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因为我是C语言的初学者

As I am a beginner in C

当我运行以下C代码时:

When I run the following C code :

#include<stdio.h> void f(int *p) { *p=3; } int main() { int *p; f(&p); return 0; }

我在编译后收到以下消息:

I get these messages after compilation:

1)警告:从不兼容的指针类型f(& p)传递'f'的arg 1

1) warning:passing arg 1 of 'f' from incompatible pointer type f(&p)

2)注意:预期为'int *',但参数的类型为'int **'void f(int * p)

2) note: expected 'int *' but argument is of type 'int **' void f(int *p)

推荐答案

这很难考虑,因为存在多个级别的指针.

It's hard to think about this, because there are multiple levels of pointers.

这是一个简单的例子.这就是我们所说的通过引用传递":

Here's a simpler example. This is what we might call "pass by reference":

#include <stdio.h> void f(int *ip) { *ip = 5; } int main(){ { int i = 7; printf("%d\n", i); f(&i); printf("%d\n", i); }

函数f接受一个指向int的指针.它修改了int.在main中,我们有一个int变量i.我们使用&获取该变量的地址,并将结果指针传递给函数f.这样,功能f可以在调用方中修改i.请注意,函数f需要一个指向某物的指针,而它所指向的是调用者中i的类型,换句话说,就是指向int的指针.

Function f accepts a pointer to an int. It modifies that int. In main, we have an int variable i. We take the address of that variable using &, and pass the resulting pointer to function f. In this way, function f is able to modify i in the caller. Note that function f takes a pointer to something, and that what it takes a pointer to is the type of i in the caller -- in other words, pointer to int.

现在让我们来看另一个例子.这次,我们将通过操纵指针变量char *来代替通过引用操作int. (我们将最终处理指向指针的指针.)

Now let's look at another example. This time, instead of manipulating an int by reference, we're going to manipulate a pointer variable, a char *. (We're going to end up dealing with pointers to pointers.)

void g(char **sp) { *sp = malloc(10); strcpy(*sp, "oranges"); } int main(){ { char *s = "apples"; printf("%s\n", s); g(&s); printf("%s\n", s); }

同样,函数g通过引用接受参数.同样,函数g能够在其调用方中修改变量.同样,g接受的指针类型是指向要操作的类型的指针.由于要操作的类型是指向char的指针",因此函数g接受指向char的指针或char **.

Again, function g accepts an argument by reference. Again, function g is able to modify a variable in its caller. Again, the pointer type accepted by g is a pointer to the type being manipulated. Since the type being manipulated is "pointer to char", function g accepts a pointer to pointer to char, or char **.

但是您发布的代码是两者的混合体.您的函数f接受一个int指针,就像我的一样,并且它试图使用该int指针来修改调用者中的int变量.但是然后,在您的main函数中,您要修改的变量不是int,而是指向int或int *的指针.

But the code you posted is sort of a mixture of the two. Your function f accepts an int pointer, just like mine, and it tries to use that int pointer to modify an int variable in the caller. But then, in your main function, the variable you're trying to modify isn't an int, it's a pointer to int, or int *.

致电时

f(&p);

您从指向int的指针p开始,并使用&取得其地址,从而得到一个指向int的指针.但是随后您正在调用函数f,该函数需要一个指向int的指针.那就是编译器试图通过消息告诉您的内容

you're starting with a pointer-to-int, p, and you're taking its address with &, resulting in a pointer-to-pointer-to-int. But then you;re calling function f, which expects a pointer-to-int. That's what the compiler is trying to tell you with the messages

警告:从不兼容的指针类型传递'f'的arg 1 注意:预期为"int *",但参数的类型为"int **"

warning: passing arg 1 of 'f' from incompatible pointer type note: expected 'int *' but argument is of type 'int **'

更多推荐

无法理解此错误

本文发布于:2023-07-27 14:03:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!