首页 \ 问答 \ 如何将值赋给指向NULL的指针(How to asign a value to a pointer that points to NULL)

如何将值赋给指向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
更新时间:2022-09-23 16:09

最满意答案

据我所知,一个星号代表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; 作品。 一元* ...
  • 正如我在评论中所说的:这显然是一个错误/不支持的功能,因此在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中的一个正常指针。例如,你可以分配内存并使指针指向新分配的区域。 你也可以指向堆栈中的变量,如: 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 ...
  • 您可以尝试使用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 ...
  • 如何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 ...
  • 实际上,我认为上述答案是错误的。 假设我们有以下目录结构: 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)。 所以它会得到 ...
  • 星号是构成类型名称的组件。 并且类型名称的组件的顺序会影响它实际的类型。 具体来说,星号会修改类型,使其成为指向左侧任何类型的指针。 所以, 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和其他语言包括允许您动态定义方法的元编程工具,因此您可以声明一个特定方法并在运行时定义其他内容,您可以在此问题上看到此功能的典型示例如何在Objective-C上实现method_missing类似的功能 。 至于另一端,实现但未声明的方法,没有明确的方法来定义Objective-C上的私有方法,并且有时您想要定义只为您自己的类而不是其他人知道的方法。 因此,您不要在.h文件中声明该方法,但是仍然在.m文件中实现它。 编译器通常会抱怨您使用的是未定义的方法,但您可以忽略此方法或将.m文 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。