user3213110
user3213110

Reputation: 199

How to get a const char* function to work?

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?

Upvotes: 1

Views: 376

Answers (2)

ntysdd
ntysdd

Reputation: 1264

If you declare const char* s, then you should not write strcat(s, " "), because strcat modifies s.

if you declare char const* reverseWordsOnly, then why do you return s[k]? s[k] is not a pointer.

EDIT: This depends on what you want to do. I don't know what you want to do in this function.

If you don't modify s, then declaring const char* s is OK.

If you want to return char const *, then maybe you want to return &s[k] instead of s[k].

Maybe you want to return char *, then you can cast &s[k] using (char *)&s[k].

Upvotes: 3

Paul Evans
Paul Evans

Reputation: 27567

It's because the const on the left-hand-side of the * means that what's pointed to is immutable: strcat obviously needs to alter its parameter. And s[k] will refer to a char, not any sort of char *.

Upvotes: 2

Related Questions