2

I'm trying to write a that function takes a const char * as a parameter, and I can't figure out how to pass this kind of data to a function in a useful way. If I have function:

void tokenize(const char * c) { }

And I want to call this function with a hardcoded value, say backslash, what is the best way to approach this? How do I turn a hardcoded value into a const char *?

4

2 回答 2

5

使用字符串文字代替const char *参数是完全合法的:

tokenize("\\");

一个更具说明性的示例是存储指向该字符串的指针,然后传递该指针:

const char *token = "\\";
tokenize(token);

但是,字符串文字与 不兼容char *,即使编译器可能允许。这与字符串文字的存储方式有关——它们通常位于内存的只读区域中,尝试写入它们会导致未定义的行为,很可能是某种保护错误。

于 2013-11-08T15:47:49.530 回答
0

您可以使用类似的字符串文字"\\"并将其作为参数传递给函数。

于 2013-11-08T15:48:14.613 回答