I like to define a method that receives a char array of variable size.
This is my current definition:
+(int) findStartIndex: (NSData*)buffer searchPattern: (char*) searchPattern;
And this is where I call it:
const char a[] = {'a','b','c'};
startIndex = [self findStartIndex:buffer searchPattern: a];
and like this
const char b[] = {'1','2'};
startIndex = [self findStartIndex:buffer searchPattern: b];
But I keep getting the compiler warning:
Sending 'const char[3]' to parameter of type 'char *' discards qualifiers
and
Sending 'const char[2]' to parameter of type 'char *' discards qualifiers
respectively.
How to do this correctly?
Because the parameter you declared as char *, but const char [] is passed. It's a have a potential risk. you should the following changes. Do not have a warning when I tested.
+(int) findStartIndex: (NSData*)buffer searchPattern: (const char*) searchPattern
Qualifiers in C apply to the keyword on the left first, then fallback to the right next. const char arr[]
is not a constant reference to a char array, it's always of type char. But, when you pass it to a method that takes a pointer to char, then you lose the const'ness of the type, and you get a warning. (Hooray for obscure C stuff!)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.