如何获得一个const char *函数工作? [英] How to get a const char* function to work?
问题描述
所以我有这个 char const * blahblahblah(const char * s)
函数,但是当我尝试使用 strcat(s,
或 return s [k]
在其中说
So I have this char const* blahblahblah(const char* s)
function but when I try to use strcat(s, " ")
or return s[k]
in it it says
const char*s
Error: argument of type "const char*" is incompatible of parameter of type "char"
如果我想让函数保持不变,我的参数应该怎么改变才能正常工作?
If I want the function to stay as is what should I change in my parameters in order for it to work?
推荐答案
如果你声明const char * s,那么你不应该写strcat(s,),因为strcat修改s。
If you declare const char* s, then you should not write strcat(s, " "), because strcat modifies s.
if你声明char const * reverseWordsOnly,那么为什么你返回s [k]? s [k]不是指针。
if you declare char const* reverseWordsOnly, then why do you return s[k]? s[k] is not a pointer.
编辑:
这取决于你想做什么。我不知道你想在这个函数中做什么。
This depends on what you want to do. I don't know what you want to do in this function.
如果你不修改s,那么声明const char * s就OK了。
If you don't modify s, then declaring const char* s is OK.
如果你想返回char const *,也许你想返回& s [k]而不是s [k]。
If you want to return char const *, then maybe you want to return &s[k] instead of s[k].
也许你想返回char *,然后你可以使用(char *)& s [k]来投射& s [k]。
Maybe you want to return char *, then you can cast &s[k] using (char *)&s[k].
这篇关于如何获得一个const char *函数工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!