0

I have a function that will pass a string and manipulate. in the function call i am passing the string as such like myfunc ("hello");

In the function definition i have

myfunc (char *array)
{
    xxxx
};

The program is working fine, but it throws a warning "pointer targets in passing argument 1 of 'myfunc' differ in signedness".

How to rectify this problem?

1
  • 1
    C or C++? The answer is the same, but the technicalities are different. Commented Mar 21, 2011 at 6:16

4 Answers 4

2

Strings are actually arrays of constant characters. That is, the type of "hello" is const char[6].

What this means is you cannot modify it. However, due to a silly conversion in C++, the array (in string literal form) can be implicitly converted to a non-const pointer to the first element. This is misleading, and dangerous. (Indeed, a const-stripping implicit conversion doesn't exist anywhere else.)

You should make sure you have a modifiable buffer instead:

char buffer[] = "hello";
myfunc(buffer);
Sign up to request clarification or add additional context in comments.

2 Comments

I think the warning is because the compiler might be treating char as unsigned at one point and signed at another. I am not very sure.
@Prasoon: Oh, probably. (I actually skimmed the question, working on getting as lazy as possible while still earning reputation. :))
0

Make sure your definition for myfunc() function has char* as parameter. I think, it has unsigned char* as parameter. or somewhere else in your code, you are passing unsigned char* as argument to myfunc(char*). Look at the warning line in your code.

Comments

0

I think Prasoon is right. Your compiler is treating "string literals" as unsigned char. Just change your function to accept unsigned char ...or change the compiler setting which decides if a "string literal" is signed/unsigned.

Comments

0

what if you declare your function like myfunc (const char *array)

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.