C++ 在C下通过引用传递指针参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1825794/
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-08-27 21:17:33  来源:igfitidea点击:

Passing pointer argument by reference under C?

c++creferencepointers

提问by Jichao

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

void
getstr(char *&retstr)
{
 char *tmp = (char *)malloc(25);
 strcpy(tmp, "hello,world");
 retstr = tmp;
}

int
main(void)
{
 char *retstr;

 getstr(retstr);
 printf("%s\n", retstr);

 return 0;
}

gccwould not compile this file, but after adding #include <cstring>I could use g++ to compile this source file.

gcc不会编译这个文件,但添加后#include <cstring>我可以使用 g++ 来编译这个源文件。

The problem is: does the C programming language support passing pointer argument by reference? If not, why?

问题是:C 编程语言是否支持通过引用传递指针参数?如果不是,为什么?

Thanks.

谢谢。

回答by Kirill V. Lyadvinsky

No, C doesn't support references. It is by design. Instead of references you could use pointer to pointer in C. References are available only in C++ language.

不,C 不支持引用。这是设计使然。您可以在 C 中使用指向指针的指针来代替引用。引用仅在 C++ 语言中可用。

回答by Bojan Resnik

References are a feature of C++, while C supports only pointers. To have your function modify the value of the given pointer, pass pointer to the pointer:

引用是 C++ 的一个特性,而 C 只支持指针。要让您的函数修改给定指针的值,请将指针传递给该指针:

void getstr(char ** retstr)
{
    char *tmp = (char *)malloc(25);
    strcpy(tmp, "hello,world");
    *retstr = tmp;
}

int main(void)
{
    char *retstr;

    getstr(&retstr);
    printf("%s\n", retstr);

    // Don't forget to free the malloc'd memory
    free(retstr);

    return 0;
}

回答by PP.

Try this:

尝试这个:


void
getstr(char **retstr)
{
 char *tmp = (char *)malloc(25);
 strcpy(tmp, "hello,world");
 *retstr = tmp;
}

int
main(void)
{
 char *retstr;

 getstr(&retstr);
 printf("%s\n", retstr);

 return 0;
}

回答by Sinan ünür

This should be a comment but it is too long for a comment box, so I am making it CW.

这应该是一条评论,但对于评论框来说太长了,所以我将其设为 CW。

The code you provided can be better written as:

您提供的代码可以更好地编写为:

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

void
getstr(char **retstr)
{
    *retstr = malloc(25);
    if ( *retstr ) {
        strcpy(*retstr, "hello,world");
    }
    return;
}

int
main(void)
{
    char *retstr;

    getstr(&retstr);
    if ( retstr ) {
        printf("%s\n", retstr);
    }
    return 0;
}

回答by Dmytro Sirenko

There is an interesting trick in libgmp which emulates references: typedef mpz_t __mpz_struct[1];

libgmp 中有一个模拟引用的有趣技巧: typedef mpz_t __mpz_struct[1];

and then you can write like this:

然后你可以这样写:

mpz_t n;
mpz_init(n);
...
mpz_clear(n);

I would not recommend to use this method, because it may be incomprehensible for others, it still does not protect from being a NULL: mpz_init((void *)NULL), and it is as much verbose as its pointer-to-pointer counterpart.

我不建议使用这种方法,因为其他人可能无法理解它,它仍然不能防止成为 NULL: mpz_init((void *)NULL),并且它与其指针到指针对应物一样冗长。

回答by Ashish

C lang does not have reference variables but its part of C++ lang.

C lang 没有引用变量,但它是 C++ lang 的一部分。

The reason of introducing reference is to avoid dangling pointers and pre-checking for pointers nullity.

引入引用的原因是为了避免悬空指针和预检查指针无效。

You can consider reference as constant pointeri.e. const pointer can only point to data it has been initialized to point.

您可以将引用视为常量指针,即常量指针只能指向它已初始化指向的数据。