为什么我可以通过这样的字符串参数?这种样式安全吗?

Why can I pass a string parameter like this? Is this style safe?

本文关键字:样式 参数 安全 字符串 可以通过 为什么      更新时间:2023-10-16

我将两个字符串作为Xcode中的一个参数和使用C 传递。我在Xcode中尝试了此操作,并且可以使用。但是这在所有平台上都安全吗?

#include <iostream>
void log_person(const char* name_and_number){
    printf("name and number : %s.n", name_and_number);
}
int main(int argc, const char * argv[]) {
    log_person("jim" "123456789");
    return 0;
}

您正在传递一个字符串。这个

"Foo" "Bar"

完全等同于

"FooBar"

所以您的呼叫与

相同
log_person("jim123456789");

这是标准的C ,因此"安全"也是如此。在任何符合的实施中。参见C 11中的 2.14.5/13 标准:

2.14.5字符串文字

...

13 在翻译阶段6(2.2)中,相邻的字符串文字被串联....