#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;
}
gcc
would not compile this file, but after adding #include <cstring>
I could use g++ to compile this source file.
gcc不会编译这个文件,但是在添加#include
The problem is: does the C programming language support passing pointer argument by reference? If not, why?
问题是:C编程语言是否支持通过引用传递指针参数?如果没有,为什么?
Thanks.
6 个解决方案
#1
29
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 ++语言中可用。
#2
20
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;
}
#3
5
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;
}
#4
2
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;
}
#5
1
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),并且它与指针到指针的对应部分一样冗长。
#6
0
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 pointer i.e. const pointer can only point to data it has been initialized to point.
您可以将引用视为常量指针,即const指针只能指向已初始化为point的数据。
#1
29
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 ++语言中可用。
#2
20
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;
}
#3
5
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;
}
#4
2
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;
}
#5
1
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),并且它与指针到指针的对应部分一样冗长。
#6
0
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 pointer i.e. const pointer can only point to data it has been initialized to point.
您可以将引用视为常量指针,即const指针只能指向已初始化为point的数据。