传递单指针,双指针的函数在C [英] Passing single pointer and double pointer to a function in c

查看:243
本文介绍了传递单指针,双指针的函数在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图传递的单一指针双指针中的函数。但它给我的错误。

I am trying to pass a single pointer and double pointer in a function. but its giving me error.

int main()
{
    int *pt;

    fun(pt);
    .
    .
}

fun(int *pt)
{
    pt=(int*)malloc(6*sizeof(int));
    .
    .
}

和什么是语法时,我们使用的是双指针。任何人可以只用一个例子来形容它,也可以编辑上面的例子。我是非常感激你。

and what is the syntax when we are using double pointer. Can anybody just describe it with an example or can edit the above example. I shall be very thankful to you.

推荐答案

引用语义的基本思想是,一个函数修改一些的其他的对象存在外设功能的自己的范围。你可以通过传递正被引用到一个类型的参数指向对象的类型的函数对象的地址实现C基准语义。

The fundamental idea of reference semantics is that a function modifies some other object that exists outside the function's own scope. You can implement reference semantics in C by passing the address of the object that is being referenced to a function that takes an argument of type "pointer to the type of the object".

的通过指针引用语义的关键标志是由这两个点:

The crucial hallmark of "reference semantics via pointers" consists of these two points:


  • 调用者需要的东西(通过&安培; - 运算符)的地址。

  • 被叫取消引用的说法。

  • The caller takes the address of something (via the &-operator).
  • The callee dereferences the argument.

例如:

来电:

T x;
f(&x);         // address-of

被叫:

void f(T * p)  // take argument as pointer
{
    (*p).y = 20;  // dereference (via *)
    p->x = 10;    // dereference (via ->)
}


在您的情况, T = INT *

int * pt;
fun(&pt);              // function call takes address

void fun(int ** p)     // function takes pointer-to-object...
{
    *p = malloc(173);  // ...and dereferences to access pointee
}

这篇关于传递单指针,双指针的函数在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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