如何将值赋给指向NULL的指针(How to asign a value to a pointer that points to NULL)
我有一个指向null的指针,我想为它赋值。 但如果我尊重它,我会得到一个错误。 我试过这个:
*nullPointer=value;
但就像我说的,我得到一个错误。 我怎样才能做到这一点?。 我做不到
nullPointer=&value
因为后来的值(它是一个对象)被删除而nullPointer将指向无效的内存。
I have a pointer that points to null, and i want to assign it a value. But if i deference it, I get an error. I have tried this:
*nullPointer=value;
but like I said, I get an error. How can I do this?. I can't do
nullPointer=&value
Because later value (it's an object) is deleted and nullPointer would point to invalid memory.
原文:https://stackoverflow.com/questions/10215615
最满意答案
据我所知,一个星号代表C中的一个正常指针。例如,你可以分配内存并使指针指向新分配的区域。 你也可以指向堆栈中的变量,如:
int x = 10; int *p = &x;
但是在某些情况下,您想要将指针传递给函数,并使函数能够将指针更改为指向另一个地址。 现在我们不是在谈论通过指针或者指向它的内存来改变变量的内容,而是通过文字将指针信息(这是一个内存地址)指向另一个地方。 在这种情况下,我们使用一个指针指向一个由双星号表示的指针。
int x, y; int *p; int **pp; pp = &p; //pp points to pointer p *pp = &x; //pp changes pointer p to point to variable x *p = 10; //p (now points to x) assigns value 10 to x *pp = &y; //pp changes pointer p again assigning the address of y *p = 20; //p assigns 20 to y; **pp = 25;// that's tricky, the first * give access to the address pointed by p (&y) the second changes the content of that address to 25. so now y ends with 25
最后x = 10和y = 25
现在看起来尤其如此,该函数调用看起来像一个将从链接列表中删除节点的函数。 这是有道理的,因为你有一个指向列表头部的外部指针。 当你删除最后一个元素时,你的函数必须能够将指针从当前地址改为null。 你可以使用全局指针来完成它,或者像你的情况那样,传递头指针的地址。
As far as I know one asterisk represents a normal pointer in C. For instance you can allocate memory and make the pointer point the that new allocated area. You can also point to variables in the stack like:
int x = 10; int *p = &x;
But there are some situations where you want to pass a pointer to a function and make the function able to change the pointer to point to another address. Now we are not talking about change the contents of a variable through a pointer nor a memory pointed by it but literary change the pointer information (which is a mem. address) to point to another places. In this case we use a pointer to a pointer which is represented by double asterisk.
int x, y; int *p; int **pp; pp = &p; //pp points to pointer p *pp = &x; //pp changes pointer p to point to variable x *p = 10; //p (now points to x) assigns value 10 to x *pp = &y; //pp changes pointer p again assigning the address of y *p = 20; //p assigns 20 to y; **pp = 25;// that's tricky, the first * give access to the address pointed by p (&y) the second changes the content of that address to 25. so now y ends with 25
In the end x = 10 and y = 25
Now looking particularly in you case, that function call looks like a function that will remove a node from a linked list. And this makes sense because you have an external pointer pointing to the head of the list. When you remove the last element your function must be able to change the pointer from the current address to null. You can do it using a global pointer or, as it seems in your case, passing the address of the head pointer.
相关问答
更多-
这里有一些允许操作员的所有这些组合以相同的方式工作。 所有这些工作的根本原因是函数(如foo )可隐式转换为指向该函数的指针。 这就是为什么void (*p1_foo)() = foo; 作品: foo被隐式地转换成一个指向自己的指针,该指针分配给p1_foo 。 一元的,当应用于函数时,产生一个指向该函数的指针,就像它在应用于对象时产生对象的地址一样。 对于普通函数的指针,由于隐式的函数到函数指针的转换,它总是冗余的。 无论如何,这就是为什么void (*p3_foo)() = &foo; 作品。 一元* ...
-
在cppclass Cython / C ++定义中是否超载被破坏?(Is overloading broken in cppclass Cython/C++ definitions?)[2022-05-21]
正如我在评论中所说的:这显然是一个错误/不支持的功能,因此在github上的Cython问题列表上报告它比在此处发布更有用。 但是 ,如果你对短期的工作感兴趣,那么下面的工作: float sum(...): return this.x + this.y float sum2 "sum"(int z): return this.x + this.y + z # A test function to prove it def test(): cdef ... -
知道在C中的方法定义中使用的星号的数量?(Knowing the number of asterisks to use in method definitions in C?)[2023-11-29]
据我所知,一个星号代表C中的一个正常指针。例如,你可以分配内存并使指针指向新分配的区域。 你也可以指向堆栈中的变量,如: int x = 10; int *p = &x; 但是在某些情况下,您想要将指针传递给函数,并使函数能够将指针更改为指向另一个地址。 现在我们不是在谈论通过指针或者指向它的内存来改变变量的内容,而是通过文字将指针信息(这是一个内存地址)指向另一个地方。 在这种情况下,我们使用一个指针指向一个由双星号表示的指针。 int x, y; int *p; int **pp; pp = &p; ... -
模板定义需要在实例化时可见。 也就是说,它需要以某种方式在标题中: // one.hpp template
static void one(T& var) { // definition visible in header } 虽然我不确定你为什么要它是静态的。 Template definitions need to be visible at the point of instantiation. That is, it needs to be in the hea ... -
突出显示方法定义(Highlight method definitions)[2023-11-11]
您可以尝试使用VS10x Editor View Enhancer : 概观 VS10x Editor View Enhancer是Visual Studio 2013/2012/2010扩展,其目标是为内置代码编辑器添加新的可视功能。 它目前具有强调类型和成员定义,块末端详细信息,可点击热点(C#和VB文档)。 You can try the VS10x Editor View Enhancer: Overview VS10x Editor View Enhancer is a Visual Studio ... -
PHP根据输入字段中给定的数字显示多个星号(PHP Displaying a number of asterisks depending on the number given in the input field)[2023-02-07]
如何str_repeat() $star = str_repeat("*", $number); 使用你当前的代码,你总是会得到更多的星号(正如@AeJey的评论中提到的那样),因为你从一个*开始,你自己加入** ,然后再加入它自己获得**** ... How about str_repeat() $star = str_repeat("*", $number); With your current code you will always get more asterixes (as mentione ... -
双星号是什么意思?(What does the double asterisks mean?)[2022-05-19]
实际上,我认为上述答案是错误的。 假设我们有以下目录结构: dbl_wc (top level) --one_level_in --aa.txt --one_level_in1 --bb.txt --deeper_dir --abc.txt Copy-Item .\dbl_wc\**\*.txt copy_target -Force只会在。\ dbl_wc下的任何目录中查找* .txt。 并且它不会查找子目录(例如。\ dbl_wc \ one_level_in1 \ deeper_dir)。 所以它会得到 ... -
之前与之后的星号?(Asterisks before vs after?)[2022-06-19]
星号是构成类型名称的组件。 并且类型名称的组件的顺序会影响它实际的类型。 具体来说,星号会修改类型,使其成为指向左侧任何类型的指针。 所以, void* 这是一个指向void的指针(即指向可能是任何类型的内存区域的指针)。 const void* 这是一个指向const void的指针(即指向内存区域的指针,该指针是 const,或者应该从该指针的视图中看作const)。 const void** 这是指向const void*的指针。 所以,考虑到这一点,你会期望这是什么: const **void ... -
重复循环三次,如下所示: for ( var i = 0; i < 3; i++ ) { // this is the line loop for ( var j = 0; j < 30; j++ ) { //this is the asterix loop document.write('*'); } document.write('
'); } 这是一个简单的演示 Repeat the loop three times, like this: for ( ... -
Objective-C中ad-hoc方法定义背后的基本原理(Rationale behind ad-hoc method definitions in Objective-C)[2023-11-13]
Objective-C和其他语言包括允许您动态定义方法的元编程工具,因此您可以声明一个特定方法并在运行时定义其他内容,您可以在此问题上看到此功能的典型示例如何在Objective-C上实现method_missing类似的功能 。 至于另一端,实现但未声明的方法,没有明确的方法来定义Objective-C上的私有方法,并且有时您想要定义只为您自己的类而不是其他人知道的方法。 因此,您不要在.h文件中声明该方法,但是仍然在.m文件中实现它。 编译器通常会抱怨您使用的是未定义的方法,但您可以忽略此方法或将.m文 ...