为什么我可以传递这样的字符串参数?这种风格安全吗?

时间:2022-02-12 01:02:25

I am passing two strings as one parameter in XCode and using c++. I tried this in XCode and it works. But is this safe on all platforms?

我在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;
}

1 个解决方案

#1


You are passing a single string. This

您传递的是一个字符串。这个

"Foo" "Bar"

is exactly equivalent to

完全等同于

"FooBar"

so your call is the same as

所以你的电话是一样的

log_person("jim123456789");

This is standard C++ and so is "safe" on any conforming implementation. See 2.14.5/13 in the C++11 standard:

这是标准的C ++,因此对任何符合标准的实现都是“安全的”。请参阅C ++ 11标准中的2.14.5 / 13:

2.14.5 String literals

2.14.5字符串文字

...

13 In translation phase 6 (2.2), adjacent string literals are concatenated....

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

#1


You are passing a single string. This

您传递的是一个字符串。这个

"Foo" "Bar"

is exactly equivalent to

完全等同于

"FooBar"

so your call is the same as

所以你的电话是一样的

log_person("jim123456789");

This is standard C++ and so is "safe" on any conforming implementation. See 2.14.5/13 in the C++11 standard:

这是标准的C ++,因此对任何符合标准的实现都是“安全的”。请参阅C ++ 11标准中的2.14.5 / 13:

2.14.5 String literals

2.14.5字符串文字

...

13 In translation phase 6 (2.2), adjacent string literals are concatenated....

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