0

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.