C语言 C 将指针分配给 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16299394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:13:16  来源:igfitidea点击:

C Assign Pointer to NULL

cfunctionpointers

提问by lynks

I am misunderstanding something basic about pointers in C, this should be simple but search brings up nothing. I do not understand the behaviour of the following code;

我误解了 C 中指针的一些基本知识,这应该很简单,但搜索什么也没带来。我不明白以下代码的行为;

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

void my_function(char *);

int main(int argc, char *argv[]) {
    char *ptr;
    ptr = malloc(10);

    if(ptr != NULL) printf("FIRST TEST: ptr is not null\n");
    else printf("FIRST TEST: ptr is null\n");

    my_function(ptr);

    if(ptr != NULL) printf("SECOND TEST: ptr is not null\n");
    else printf("SECOND TEST: ptr is null\n");
}

void my_function(char *a) {
    a = NULL;
}

Which outputs;

哪些输出;

FIRST TEST: ptr is not null
SECOND TEST: ptr is not null

Why does the second test still see the pointer as not NULL? I am trying to use a NULL pointer assignment as a sort of 'return flag' to indicate a certain failure of the function. But upon testing the pointer afterwards, it does not seem to be NULL.

为什么第二个测试仍然看到指针不是 NULL?我试图使用 NULL 指针赋值作为一种“返回标志”来指示函数的某个失败。但是在之后测试指针时,它似乎不是 NULL。

回答by Some programmer dude

It's because the pointer is passed by value and not by reference. If you want to change the pointer inside the function you need to pass the actual pointer as a pointer, i.e. a pointer to a pointer:

这是因为指针是按值传递的,而不是按引用传递的。如果要更改函数内部的指针,则需要将实际指针作为指针传递,即指向指针的指针:

void my_function(char **a)
{
    *a = NULL;
}

Use the address-of operator &when you call the function to get the address of the pointer:

&调用函数时使用 address-of 运算符来获取指针的地址:

my_function(&ptr);

回答by Rüppell's Vulture

Your statement a=NULLin my_function()indeed sets the value of ato NULL, but ais a local variable of that function.When you passed ptrto my_function()in main(), the value of ptrwas copied to a.I suppose your whole confusion arose from the *used before ain the definition of my_function().

你的声明a=NULLmy_function()确实设置的值aNULL,但是a是你通过了功能。当一个局部变量ptrmy_function()main(),价值ptr被复制到a。我想你的整个混乱从产生*使用之前a的定义my_function()

Pointers are generally passed to functions when we want to manipulate the original values which those pointers point to, from the called function, and this is done by dereferencingthose pointers from the called functions.In this case, had you used this:

当我们想要从被调用函数中操作这些指针指向的原始值时,通常会将指针传递给函数,这是由dereferencing来自被调用函数的那些指针完成的。在这种情况下,您是否使用过这个:

*a= blah blah;

it would have reflected in the value at the address pointed to by ptrin main().But since you want to change the value of ptritself, you need to be able to have a way to manipulateit from my_function().For this you use a pointer-to-pointer,ie of type char**.You pass such a char**as argument to my_function(()and use it to alter the value of ptr.Here's the variation to your code that would do it for you:

它会反映在通过指向地址的值ptrmain(),因为你想改变的价值。但ptr本身,你需要能够有办法manipulate从它my_function()。对于这一点,你使用pointer-to-pointer,即类型char**。您将诸如char**as 参数传递给my_function(()并使用它来更改 的值。ptr这是为您执行此操作的代码的变体:

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

void my_function(char **); // Change char* to char**

int main(int argc, char *argv[]) {
    char *ptr;
    ptr = malloc(10);

    if(ptr != NULL) printf("FIRST TEST: ptr is not null\n");
    else printf("FIRST TEST: ptr is null\n");

    my_function(&ptr); //You pass a char**

    if(ptr != NULL) printf("SECOND TEST: ptr is not null\n");
    else printf("SECOND TEST: ptr is null\n");
}

void my_function(char **a) {  //Change char* to char** here
    *a = NULL;
}

回答by richselian

in C, a function call like foo(a)will never change the value of a.

在 C 中,像这样的函数调用foo(a)永远不会改变 a 的值。

回答by Tony The Lion

Your function should take a char** aif you want to modify what it points to. This is because pointers are copiedas arguments to a function, meaning that any changes you make to it inside, will not be seen outside the function, as its modifying a copy.

char** a如果您想修改它指向的内容,您的函数应该采用 a 。这是因为指针被复制为函数的参数,这意味着您在内部对其所做的任何更改都不会在函数外部被看到,因为它修改了一个副本。

If you want to change it and see it outside the function scope, you need to add another indirection.

如果你想改变它并在函数范围之外看到它,你需要添加另一个间接。

回答by vszurma

when passing the pointer to the function the pointer is copied into functions scope. you need to use a pointer of pointer if you want do such things. A pointer is basicly only an integer/long

当将指针传递给函数时,指针被复制到函数作用域中。如果你想做这样的事情,你需要使用指针指针。指针基本上只是一个整数/长整数

回答by user2054656

Your problem is, that my_pointergets does not write to the pointer ptr, but its copy, *a.

您的问题是,my_pointer它不会写入指针ptr,而是写入其副本*a.

You need to pass the address of ptrdo to what you want.

您需要将ptrdo的地址传递给您想要的。