如何通过箭头指针到功能? [英] How to pass arrow pointer into function?

查看:142
本文介绍了如何通过箭头指针到功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

void allocateMemory(struct node *some_node) {
    some_node = malloc(sizeof(struct node));
}

在另一个功能:

struct node add(struct node *root, struct node *thisNode, int value)

我尝试调用这样的:

I try to call this:

allocateMemory(thisNode->next);

我得到一个运行时错误。它什么都不做。
然而,当我做同样的事情,allocateMemory()在上述功能,即:

I get a runtime error. It does nothing. Yet when I do the same thing as allocateMemory() in the said function, i.e:

thisNode->next = malloc(sizeof(struct node));

它做什么是应该做的。
我在做什么错了?

It does what it is supposed to do. What am I doing wrong?

推荐答案

您需要PAAS的指针的指针

当你有指针,那么你就可以的变更的值是指针指向,当你想改变实际的指针,那么你需要走一步深入。

When you have pointer, then you can change value which is that pointer pointing to, and when you want to change the actual pointer then you need go one step deeper.

另外添加的功能应该没有返回值,但指针?

Also function add shouldn't return value but pointer?

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

void allocateMemory(struct node **some_node) {
  *some_node = (struct node*)malloc(sizeof(struct node));
}

struct node* add(struct node *root, struct node *thisNode, int value) {
  allocateMemory(&thisNode->next);
  thisNode->x = value;

  root->next = thisNode;

  return thisNode;
}

这篇关于如何通过箭头指针到功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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