使用->访问指向struct的指针的指针操作员 [英] Accessing pointer to pointer of struct using -> operator

查看:101
本文介绍了使用->访问指向struct的指针的指针操作员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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

struct node
{
    int data;
    struct node *next;
};

void pointerOfPointer(struct node **reference)
{
    struct node *temporary = malloc(sizeof(struct node));

    temporary->data = 100;
    temporary->next = 0;

    printf("before: temporary->data %d\n", temporary->data);
    temporary = *reference;
    printf("after: temporary->data %d\n", temporary->data);
}

int main()
{
    struct node *linkedlist = malloc(sizeof(struct node));
    linkedlist->data = 15;
    linkedlist->next = 0;
    pointerOfPointer(&linkedlist);
    return 0;
}

如何在没有将*参考地址复制到临时局部变量的情况下,在pointerOfPointer函数中访问指向struct的指针?因此,最后我可以使用运算符->直接访问参考变量数据,例如reference-> data?

How can I access the pointer to pointer of struct in the pointerOfPointer function without copying the *reference address to the temporary local variable? So in the end I can access the reference variable data using operator -> directly, like reference->data?

推荐答案

请记住,foo->bar只是(*foo).bar的语法糖.您要求的实际上是(**reference).data,如果需要,可以将其重写为(*reference)->data.

Remember that foo->bar is just syntactic sugar for (*foo).bar. What you're asking for is essentially (**reference).data, which you can rewrite as (*reference)->data if you want.

这篇关于使用-&gt;访问指向struct的指针的指针操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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