[英]C++ Pointer to Pointer to Function Parameter
the following C++ code does not work as I wish. 以下C ++代码无法正常运行。
#include <string>
#include <iostream>
int Pointer_Function(char* _output);
int Pointer_to_Pointer_Function(char** text );
int main() {
char* input = "This";
printf("1. %s \n", input);
Pointer_Function(input);
printf("5. %s \n", input);
int test;
std::cin >> test;
}
int Pointer_Function(char* _output) {
_output = "Datenmanipulation 1";
printf("2. %s \n", _output);
Pointer_to_Pointer_Function(&_output);
printf("4. %s \n", _output);
return 0;
}
int Pointer_to_Pointer_Function(char** text ) {
printf("3. %s \n", *text);
char* input = "HalliHallo";
text = &input;
printf("3.1. %s \n", *text);
return 0;
}
I wish as result for printf 5. HalliHallo instead of Datenmanipulation. 我希望作为printf 5的结果。HalliHallo代替Datenmanipulation。 Because data text must be changed due to &input .
因为必须通过&input更改数据文本 。
Output: 输出:
1.This
2. Datenmanipulation 1
3. Datenmanipulation 1
3.1 HalliHallo
4. Datenmanipulation 1
5. This
Expected Result: 预期结果:
1.This
2. Datenmanipulation 1
3. Datenmanipulation 1
3.1 HalliHallo
4. HalliHallo
5. HalliHallo
How can I give pointer to pointer to a Function as a Parameter? 我怎样才能给一个指向函数的指针作为参数? Why does not work my Code?
为什么我的代码不起作用?
When you assign: 分配时:
text = &input;
you're just changing the local variable text
, you're not changing the contents of the pointer that it pointed to. 您只是在更改局部变量
text
,而没有更改它指向的指针的内容。 You should do: 你应该做:
*text = input;
This will make it print: 这将使其打印:
4. HalliHallo
You can't make it print 您无法打印
5. HalliHallo
because Pointer_Function
just takes a pointer to the string, not a pointer to the variable, so it can't change the caller's variable. 因为
Pointer_Function
仅使用指向字符串的指针,而不是指向变量的指针,所以它无法更改调用者的变量。
You should also change all your declarations to specify const char*
and const char**
, since you're assigning pointers to literal strings. 您还应该更改所有声明以指定
const char*
和const char**
,因为您正在将指针分配给文字字符串。 Here's the fully working code: 这是完整的工作代码:
#include <string>
#include <iostream>
int Pointer_Function(const char* _output);
int Pointer_to_Pointer_Function(const char** text );
int main() {
const char* input = "This";
printf("1. %s \n", input);
Pointer_Function(input);
printf("5. %s \n", input);
int test;
std::cin >> test;
}
int Pointer_Function(const char* _output) {
_output = "Datenmanipulation 1";
printf("2. %s \n", _output);
Pointer_to_Pointer_Function(&_output);
printf("4. %s \n", _output);
return 0;
}
int Pointer_to_Pointer_Function(const char** text ) {
printf("3. %s \n", *text);
const char* input = "HalliHallo";
*text = input;
printf("3.1. %s \n", *text);
return 0;
}
Output: 输出:
1. This
2. Datenmanipulation 1
3. Datenmanipulation 1
3.1. HalliHallo
4. HalliHallo
5. This
You can fix the last function by altering the value of the pointer the pointer char** is pointing to: 您可以通过更改指针char **指向的指针的值来修复最后一个函数:
C C
int Pointer_to_Pointer_Function(char** text ) {
*text = "HalliHallo";
return 0;
}
To fix both function you might pass by reference: 要修复这两个功能,您可以通过引用传递:
C++ C ++
int Reference_to_Pointer_Function(char*& text ) {
text = "HalliHallo";
return 0;
}
In C++11 a string literal has the (decayed) type 'char const*' (the conversion to char* is deprecated 在C ++ 11中,字符串文字的类型为'char const *'(已衰减)(不建议转换为char *
The problem is in you Pointer_to_pointer_function 问题出在你身上Pointer_to_pointer_function
int Pointer_to_Pointer_Function(char** text ) {
printf("3. %s \n", *text);
char* input = "HalliHallo";
text = &input;
printf("3.1. %s \n", *text);
return 0;
}
You pass a pointer to pointer to char as argument to the function. 您将指向char的指针的指针作为函数的参数传递。 The variable input is pointer to char .
输入的变量是指向char的指针 。 Because of that
text = &input;
因为那个
text = &input;
is not correct. 是不正确的。 You are assigning the address of a pointer to a pointer to pointer to char.
您正在将指针的地址分配给指向char的指针。 The assignment should be
*text = &input;
分配应为
*text = &input;
You probably notice there is no need to use pointer to pointer here; 您可能会注意到这里不需要使用指向指针的指针。 not for what you are doing.
不是因为你在做什么。 You would need it if you were passing an in/out parameter to the function.
如果要将in / out参数传递给函数,则将需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.