English 中文(简体)
为什么在发挥推力作用时用点子指向脚点?
原标题:Why use a pointer to a pointer to the stack when creating a push function?

我正在研究一个相关清单的课本例子,该清单将实施一套分寸。 我不理解为什么要把点子用到点子上,才能推进行动。 见以下例子:

bool push( Element **stack, void *data)
{
    Element *elem = new Element;
    if(!elem) return false;

    elem->data = data;
    elem->next = *stack;
    *stack = elem;
    return true;
}

如果任何人能够帮助澄清为什么推进方法的第一参数是点人,我将非常赞赏。 感谢。

令人惊讶的是,感谢你给予一切出色帮助。

最佳回答

该职能需要修改执行点的价值,因此需要一个点到,点。

换言之:当职能需要修改时,它就具有一点点。

在这种情况下,这本身就是一个要点。 因此,这项职能最终要有一个点。

问题回答

点击器只是一个具有价值的的变量,这个数值是一个记忆地址。

指点人也是具有价值的变量。 这一价值是一位点人的记忆。

当你想要改变点子的时,你使用点子。

//Not a very useful example, but shows what I mean...
void getOffsetBy3Pointer(const char *pInput, char **pOutput)
{
  *pOutput = pInput + 3;
}

你也称这一职能类似:

const char *p = "hi you";
char *pYou;
getOffsetBy3Pointer(p, &pYou);
assert(!stricmp(pYou, "you"));

我们现在考虑的是,如果我们试图以单一点来执行这一职能,将会发生什么。

//Note: This is completely wrong
void BadGetOffsetBy3Pointer(const char *pInput, char *pOutput)
{
  //*pOutput refers to the first actual char element that pOutput points to.
  pOutput = pInput + 3;
  //pOutput now points to pInput + 3, but the variable we passed in remains distinct.
}

你也称这一职能类似:

const char *p = "hi you";
char *pYou = NULL;
BadGetOffsetBy3Pointer(p, pYou);
assert(pYou == NULL);

在BadGetOffsetBy3 Pointer的说明中,我们可以改变一些特性,但我们可以不改变你所说的话。

A stack is basically a linked list of pointers. Each one pointing to the one below it. Because you have a new element and you want that element to come first in your list (hence the term "stack", you have to change what points to the start of your list.

To change the value in the "pointer to the head of the list", you need the Address of that pointer.

因此:

    *stack = elem;

基本上,你正在对该职能的原点人进行修改。

各位必须更新一名协调人。

A list is nothing but a pointer to an Element. So you may rewrite this to

bool push(List* stack, void* data);

你们不认为,如果你不使用双点,你的实际声明就是

bool push(List stack, void* data);

这根本不会改变原来的名单。

但轮流,

bool push(Element* &stack, ...)

这也是事实,因为它允许你更新。

右翼是最后一点的点子。 为了改变僵局,把一个因素推向前进,必须更新这个要点,以便我们把一个点推到推动功能上。

审视一个人如何利用这一推动职能:

Data d1, d2;

Stack *s = null; // s := null
push(&s, &d1); // s := {d1}->null
push(&s, &d2); // s:= {d2}->{d1}->null

现在,你可以继续使用经修改的变量,将其推向增长的顶端。

在 st中,你总是希望保持顶点,而推波功能使保持这一点更为容易。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签