如何在带参数的函数中调用变量函数?(How can I call a variable function within a function with parameters?)
我试图在函数内调用一个变量函数,但它没有按预期工作。
这是我的代码:
$arg1 = "Beef"; $arg2 = "Potatoes"; $string1 = "Tomatoes"; $string2 = "Onions"; function func1 ($arg1, $arg2){ echo $arg1.' '.$arg2; } $var1 = "func1"; function func2($string1, $string2, $var1) { echo $string1." & ".$string2; $var1($arg1, $arg2); } func2($string1, $string2, $var1);
当我调用
func2
,它只回应“Tomatoes&Onions”Isnt$arg1
和$arg2
global? 为什么在函数func2
没有看到它们? 如何在func2
调用$var1
及其参数?I am trying to call a variable function within a function but it is not working as expected.
Here is my code:
$arg1 = "Beef"; $arg2 = "Potatoes"; $string1 = "Tomatoes"; $string2 = "Onions"; function func1 ($arg1, $arg2){ echo $arg1.' '.$arg2; } $var1 = "func1"; function func2($string1, $string2, $var1) { echo $string1." & ".$string2; $var1($arg1, $arg2); } func2($string1, $string2, $var1);
When I call
func2
, it only echoes "Tomatoes & Onions" Isnt$arg1
and$arg2
global? Why are they not seen inside the functionfunc2
? How can I call$var1
insidefunc2
along with its arguments?
原文:
最满意答案
&pointerToFirstChar
和&pointerToSecondChar
,你没有获取char
数组元素的地址,而是获取了本地变量pointerToFirstChar
和pointerToSecondChar
的地址。 请注意,他们自己一直在指点。你可能想要:
cout << "Value was " << pointerToFirstChar << ", address: " << static_cast<void*>(pointerToFirstChar) << endl; cout << "Value 2 was " << pointerToSecondChar << ", address:" << static_cast<void*>(pointerToSecondChar) << endl;
请注意,您需要将它们转换为
void*
以打印出地址而不是字符串。
&pointerToFirstChar
and&pointerToSecondChar
, you're not taking the address of the elements of thechar
array, but the address of the local variablepointerToFirstChar
andpointerToSecondChar
. Note they've been pointers themselves.You might want:
cout << "Value was " << pointerToFirstChar << ", address: " << static_cast<void*>(pointerToFirstChar) << endl; cout << "Value 2 was " << pointerToSecondChar << ", address:" << static_cast<void*>(pointerToSecondChar) << endl;
Note you need to cast them to
void*
to print out the address instead of the string.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
也许你需要使用strtok函数: http ://www.cplusplus.com/reference/cstring/strtok/。 Perhaps you need to use strtok function: http://www.cplusplus.com/reference/cstring/strtok/.
-
当您在堆栈中声明char数组时,其值不会初始化为0,这意味着这些插槽包含此时堆栈中的任何内容。 换句话说,令人惊讶的部分不是9处的行为,而是每隔一个值,因为这个char数组在覆盖它们之前的内容纯粹是未定义的。 如果你想要某个结果,你必须自己初始化为0。 When you declare your char array in the stack, its values are not initialized at 0, meaning those slots contain whatever was on t ...
-
计算char数组中包含空格的字符数,直到null char(Count number of chars in char array including spaces until null char)[2022-04-26]
也许我错过了一些东西,但为什么不呢: int numberOfCharsInArray(char* array) { return strlen(array); } ...甚至: int numberOfCharsInArray(char* array) { return std::string(array).length(); } Perhaps I'm missing something, but why not just: int numberOfCharsInArray(char* ... -
由于char是一种原始类型,因此一个字符数组将直接将这些字节存储在数组中,而且每个字符都没有开销。 相比之下, String是一个对象,因此该数组将存储指向堆中其他地方的String实例的指针,每个指针都有自己的vtable,length和其他信息开销(包括对char[]的单独引用)文本)。 拥有大量对象也会增加GC堆碎片的风险。 另外,如果你通过串联而不是StringBuilder来构建字符串,你还会得到大量额外的副本,占用更多的内存。 Since char is a primitive type, an ...
-
尝试更像这样的东西: #include
#include #include char ** board = new char *[row]; for (int r = 0; r < row; r++) { board[r] = new char[col]; } for (int r = 0; r < row; r++) { std::cout << "Enter input: " << std::endl; std::c ... -
removechars接受所有字符串类型(char [],wchar [],dchar [],string,wstring和dstring),但第二个参数必须与第一个相同。 因此,如果您将char []作为第一个arg传递,则第二个arg也必须是char []。 但是,你传递一个字符串:“” 一个简单的解决方案是将字符串复制到char []:“”.dup removechars(line,“”.dup) 这也有效: removechars(line,['\ x20']) removechars accept ...
-
我是否需要在我的字符串数组中包含终止字符的空间?(Do I need to include space for the terminating char in my string array?)[2022-01-14]
这取决于你的目标是什么: 存储一个包含10个char的数组: char someChars[10]; 存储一个最大字符串。 10个char : char myString[11]; /* 10 + '\0' */ 请注意,该字符串不是C语言类型。 It depends according to what your goal is: Store an array of 10 chars: char someChars[10]; Store a string of max. 10 chars: char ... -
在c中解析char数组(Parsing a char array in c)[2024-04-22]
字符串"[1234 abcd]"需要大小为12(至少)的字符数组。 在parse内部,你将它复制到11号数组中。为什么? 此外,您的ip数组以第一个大小1声明,这意味着访问ip[1]是非法的。 您只能访问ip[0] 。 换句话说,您的代码在几个地方无可救药地超越阵列内存。 行为未定义。 在您修复上述错误(以及任何其他此类错误)之前,任何有关代码运行时行为的问题都没有任何意义。 String "[1234 abcd]" requires a char array of size 12 (at least). ... -
为什么char似乎在数组中占用更多的空间而不是自身(Why does a char seem to take more space in an array than by itself)[2023-05-28]
&pointerToFirstChar和&pointerToSecondChar ,你没有获取char数组元素的地址,而是获取了本地变量pointerToFirstChar和pointerToSecondChar的地址。 请注意,他们自己一直在指点。 你可能想要: cout << "Value was " << pointerToFirstChar << ", address: " << static_cast(pointerToFirstChar) << endl; cout << "Val ...