我碰巧对WordPress博客进行了一些更改,并注意到他们使用parse_str(http://php.net/parse_str)解析并将其可选参数设置为函数.
我想知道发送阵列是否有优势?
例子:
有阵列:
$blahOptions = array(
'option_1' => true,
);
BlahArray($blahOptions);
function BlahArray($options = array()) {
$defaults = array(
'option_1' => false,
'option_2' => 'blah',
);
// this would probably be in a function to be used everywhere
foreach ($defaults as $defaultOption => $defaultValue) {
if (!isset($options[$defaultOption])) $options[$defaultOption] = $defaultValue;
}
}
Run Code Online (Sandbox Code Playgroud)
用parse_str:
$blahOptions = 'option_1=1';
BlahString($blahOptions);
function BlahString($options = '') {
$defaults = array(
'option_1' => false,
'option_2' => 'blah',
);
parse_str($options, …Run Code Online (Sandbox Code Playgroud) 在x86-32汇编中,参数存储在堆栈中,但在x86-64中,参数存储在寄存器中.这是什么原因?
我正在与C2751编译器错误搏斗,并不太明白究竟是什么导致它.以下小代码产生错误:
#include <iostream>
class A {
public:
A () { std::cout << "A constructed" << std::endl; };
static A giveA () { return A (); }
};
class B {
public:
B (const A& a) { std::cout << "B constructed" << std::endl; }
};
int main () {
B b1 = B (A::giveA ()); // works
B b2 (B (A::giveA ())); // C2751
B b3 (A::giveA ()); // works
}
Run Code Online (Sandbox Code Playgroud)
编译器输出:
consoleapplication1.cpp(21): error C2751: 'A::giveA': the name of …
我正在尝试编写一个函数来交换2D数组中的2个元素:
void swap(int surface[][], int x1, int y1, int x2, int y2) {
int temp = surface[x1][y1];
surface[x1][y1] = surface[x2][y2];
surface[x2][y2] = temp;
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译它(gcc)时,我收到此错误消息:
Sim_Annealing.c: In function `swap':
Sim_Annealing.c:7: error: invalid use of array with unspecified bounds
Sim_Annealing.c:8: error: invalid use of array with unspecified bounds
Sim_Annealing.c:8: error: invalid use of array with unspecified bounds
Sim_Annealing.c:9: error: invalid use of array with unspecified bounds
Run Code Online (Sandbox Code Playgroud)
为了将2D数组作为函数参数,我是否需要做一些特殊的魔术?
谢谢你的帮助.如果你知道有关数组的任何好的引用作为函数参数发送我的方式:)
我有一个PHP函数,在回显一个字符串时,正在丢失一些传递给它的字符.有什么理由会发生这种情况吗?
我通过: $vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX
它返回: #jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX
这是我的PHP代码:
<?php
function display($str) {
echo $str;
}
display("$vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX");
?>
Run Code Online (Sandbox Code Playgroud) 我环顾四周,看到了其中的一些,但没有一个为我的问题提供解决方案.我使用以下代码获得此编译错误:
错误:

代码:
const int TOP_WORDS = 25;
...
void topWords(Hash t, string word, string topA[]);
int main()
{
...
Hash table1;
string word = "example";
string topWordsArr[TOP_WORDS];
table1.addItem(word);
topWords(table1, word, topWordsArr);
...
}
...
void topWords(Hash t, string word, string topA[])
{
int i = 0;
int tempCount = t.itemCount(word);
int tempCount2 = t.itemCount(topA[i]);
while (tempCount > tempCount2 && i < TOP_WORDS) {
i++;
tempCount2 = t.itemCount(topA[i]);
}
if (i > 0)
Run Code Online (Sandbox Code Playgroud)
我看到的关于这个错误的所有其他帖子都涉及声明/传递字符串数组参数的错误语法,但我已经对它进行了双重和三重检查,我确信它是正确的; 虽然我以前错了..
我只是学习编译嵌入式C.我看到一些代码如下.
该函数定义如下:
void printDebug(const char d1[]){(void)d1;}
Run Code Online (Sandbox Code Playgroud)
它使用如下:
printDebug("BLE_UART_EVENT");
Run Code Online (Sandbox Code Playgroud)
我不明白它的目的.它给我一个可调用char数组的印象?
我和我的朋友在我们的代码中使用结构(我们的代码彼此分开).让我们举个例子:
struct Book {
char title;
int pages;
}
void setBook(struct Book *tempBook) {
tempBook->title = "NiceTitle";
tempBook->pages = 50;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码很简单.问题是,拥有这两个主电源是否有任何区别:
int main() {
struct book obj1;
setBook(&obj);
}
Run Code Online (Sandbox Code Playgroud)
要么
int main() {
struct Book *obj2;
setBook(obj2);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我的发言中并不清楚.我已经把品酒初始化了
struct Book *obj2 = malloc(sizeof(struct obj2));
Run Code Online (Sandbox Code Playgroud) 在函数内部重新分配函数参数是不好还是好的做法,或者可能是未定义的行为?
让我用一个例子解释一下我想要做什么,这里是函数:
void
gkUpdateTransforms(GkNode *node /* other params */) {
GkNode *nodei;
if (!(nodei = node->chld))
return;
do {
/* do job */
nodei = nodei->next;
} while (nodei);
}
Run Code Online (Sandbox Code Playgroud)
选择:
void
gkUpdateTransforms2(GkNode *node /* other params */) {
/* node parameter is only used here to get chld, not anywhere else */
if (!(node = node->chld))
return;
do {
/* do job */
node = node->next;
} while (node);
}
Run Code Online (Sandbox Code Playgroud)
我检查了程序集输出,看起来是一样的,我们不需要在第二个输出中声明变量。您可能会问,如果参数类型发生变化,但第一个条件相同怎么办,因为它也需要更新。
编辑:参数是按值传递的,我的目的不是编辑指针本身
EDIT2:递归函数怎么样?如果 gkUpdateTransforms2 是递归的,会发生什么?我很困惑,因为函数会调用自身,但我认为在每次调用中,参数都会有不同的堆栈
c ×3
c++ ×3
function ×3
arrays ×2
php ×2
assembly ×1
c89 ×1
c99 ×1
casting ×1
constructor ×1
pointers ×1
struct ×1
texttrimming ×1
visual-c++ ×1
x86-64 ×1