1

According to the ISO C Standard (6.3.16.1), a pointer can only be assigned to another pointer if "both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all of the qualifiers of the type pointed to by the right". I got that warning in my static analyses tool, But I am not sure if there is a real problem or not. Because as you can see at the code below, that pointer will be passed to function that has "const" qualifier in it's prototype

int main() { static volatile const signed int batu[5] = {1,2,3,4,5}; unsigned int x = 5; func(x, (signed int *) batu); } signed int func(unsigned int p1, volatile const signed int *p2) { return 0; } 
10
  • There is no section 6.3.16, less a .1 in the standard. What ever you use as reference is not ISO9899. Commented Apr 27, 2016 at 14:29
  • Never use casts if 1) they are not absolutely required or 2) you don't understand all implications of the cast or 3) you don't accept them completely. So, why do you use that cast? Commented Apr 27, 2016 at 14:31
  • It's section 6.5.16.1/1 (C99) Commented Apr 27, 2016 at 14:31
  • @atturri: C99 is not C standard! Commented Apr 27, 2016 at 14:32
  • 1
    @Olaf. I don't mind if it's the current standard or not. It is the standard where the quote in the question can be read. You will not find that quote in the latest C standard. Commented Apr 27, 2016 at 14:39

1 Answer 1

1

You just need to remove the (signed int *) cast you have in your call to the function. Variable batu and the argument p2 expected at the function are of the same type (see below), qualifiers included, so you don't need a cast at all.

An array type is always automatically converted to its equivalent pointer type except when it's an input to the sizeof or unary & operators. From the C standard 6.3.2.1/3.

Sign up to request clarification or add additional context in comments.

10 Comments

Atturi you are quite right but this code generated by matlab and changing it manually is not suggested. Actually I am trying to understand if that error is genuine or not
@batgun: That error is indeed critical. And IMHO a tool which generates unnecessary and (potentially) dangerous casts should be trashed asap. That is even more true for widely used tools.
@batgun In this particular case, if you have not left out any relevant code, it is false alarm. But if code uses pointer for which volatile has been removed for other purposes, it can be potentially harmful.
@olaf what do you think about prototype of func(). It has volatile const in it's definition. So it will re-gain const qualifier in that function? For example when I try to change value in that function scope,compiler is angry withme
@batgun Yes, if you pass pointer to unqualified type as function argument, and argument is pointer to qualified type, then those qualifications are "gained" for the duration of the function, as long as you access it through that argument variable.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.