Click here to Skip to main content
15,167,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When making the following code:
#include <general.dep>

typedef struct {
} obj;

void f1(obj **ppo) {}
void f2(const obj **ppo) {}
void f3(obj *const *ppo) {}
void f4(obj **const ppo) {}
void f5(const obj *const *ppo) {}
void f6(const obj **const ppo) {}
void f7(obj *const *const ppo) {}
void f8(const obj *const *const ppo) {}

void g1(obj *po) {}
void g2(const obj *po) {}
void g3(obj *const po) {}
void g4(const obj *const po) {}

int main() {
  obj o, *po, **ppo;

  f1(&po);
  f2(&po);
  f3(&po);
  f4(&po);
  f5(&po);
  f6(&po);
  f7(&po);
  f8(&po);

  f1(ppo);
  f2(ppo);
  f3(ppo);
  f4(ppo);
  f5(&po);
  f6(&po);
  f7(&po);
  f8(&po);

  g1(&o);
  g2(&o);
  g3(&o);
  g4(&o);

  g1(po);
  g2(po);
  g3(po);
  g4(po);

  return 0;
}

I get the following warning messages:
Quote:
gcc t1.c -I/home/hamidi/code/lib/general/inc -fmax-errors=1 -lgeneral -llzma -pthread -o t1
t1.c: In function ‘main’:
t1.c:24:6: warning: passing argument 1 of ‘f2’ from incompatible pointer type [-Wincompatible-pointer-types]
24 | f2(&po);
| ^~~
| |
| obj ** {aka struct <anonymous> **}
t1.c:7:21: note: expected ‘const obj **’ {aka ‘const struct <anonymous> **’} but argument is of type ‘obj **’ {aka ‘struct <anonymous> **’}
7 | void f2(const obj **ppo) {}
| ~~~~~~~~~~~~^~~
t1.c:27:6: warning: passing argument 1 of ‘f5’ from incompatible pointer type [-Wincompatible-pointer-types]
27 | f5(&po);
| ^~~
| |
| obj ** {aka struct <anonymous> **}
t1.c:10:27: note: expected ‘const obj * const*’ {aka ‘const struct <anonymous> * const*’} but argument is of type ‘obj **’ {aka ‘struct <anonymous> **’}
10 | void f5(const obj *const *ppo) {}
| ~~~~~~~~~~~~~~~~~~^~~
t1.c:28:6: warning: passing argument 1 of ‘f6’ from incompatible pointer type [-Wincompatible-pointer-types]
28 | f6(&po);
| ^~~
| |
| obj ** {aka struct <anonymous> **}
t1.c:11:27: note: expected ‘const obj ** const’ {aka ‘const struct <anonymous> ** const’} but argument is of type ‘obj **’ {aka ‘struct <anonymous> **’}
11 | void f6(const obj **const ppo) {}
| ~~~~~~~~~~~~~~~~~~^~~
t1.c:30:6: warning: passing argument 1 of ‘f8’ from incompatible pointer type [-Wincompatible-pointer-types]
30 | f8(&po);
| ^~~
| |
| obj ** {aka struct <anonymous> **}
t1.c:13:33: note: expected ‘const obj * const* const’ {aka ‘const struct <anonymous> * const* const’} but argument is of type ‘obj **’ {aka ‘struct <anonymous> **’}
13 | void f8(const obj *const *const ppo) {}
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~
t1.c:33:6: warning: passing argument 1 of ‘f2’ from incompatible pointer type [-Wincompatible-pointer-types]
33 | f2(ppo);
| ^~~
| |
| obj ** {aka struct <anonymous> **}
t1.c:7:21: note: expected ‘const obj **’ {aka ‘const struct <anonymous> **’} but argument is of type ‘obj **’ {aka ‘struct <anonymous> **’}
7 | void f2(const obj **ppo) {}
| ~~~~~~~~~~~~^~~
t1.c:36:6: warning: passing argument 1 of ‘f5’ from incompatible pointer type [-Wincompatible-pointer-types]
36 | f5(&po);
| ^~~
| |
| obj ** {aka struct <anonymous> **}
t1.c:10:27: note: expected ‘const obj * const*’ {aka ‘const struct <anonymous> * const*’} but argument is of type ‘obj **’ {aka ‘struct <anonymous> **’}
10 | void f5(const obj *const *ppo) {}
| ~~~~~~~~~~~~~~~~~~^~~
t1.c:37:6: warning: passing argument 1 of ‘f6’ from incompatible pointer type [-Wincompatible-pointer-types]
37 | f6(&po);
| ^~~
| |
| obj ** {aka struct <anonymous> **}
t1.c:11:27: note: expected ‘const obj ** const’ {aka ‘const struct <anonymous> ** const’} but argument is of type ‘obj **’ {aka ‘struct <anonymous> **’}
11 | void f6(const obj **const ppo) {}
| ~~~~~~~~~~~~~~~~~~^~~
t1.c:39:6: warning: passing argument 1 of ‘f8’ from incompatible pointer type [-Wincompatible-pointer-types]
39 | f8(&po);
| ^~~
| |
| obj ** {aka struct <anonymous> **}
t1.c:13:33: note: expected ‘const obj * const* const’ {aka ‘const struct <anonymous> * const* const’} but argument is of type ‘obj **’ {aka ‘struct <anonymous> **’}
13 | void f8(const obj *const *const ppo) {}
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~

How can I resolve these warning issues?

Update:

Let's discuss about these prototypes. What are their meanings and what are their differences?

void f0(obj **ppo) {}
void f1(const obj **ppo) {}	 //
void f2(obj const **ppo) {}	 //
void f3(obj *const *ppo) {}
void f4(obj **const ppo) {}
void f5(const obj const **ppo) {}  //
void f6(const obj *const *ppo) {}  //
void f7(const obj **const ppo) {}  //
void f8(obj const *const *ppo) {}  //
void f9(obj const **const ppo) {}  //
void fa(obj *const *const ppo) {}
void fb(const obj const *const *ppo) {}		   //
void fc(const obj const **const ppo) {}		   //
void fd(const obj *const *const ppo) {}		   //
void fe(obj const *const *const ppo) {}		   //
void ff(const obj const *const *const ppo) {}  //


The ones tagged with // cause the warning to be issued.

What I have tried:

Essentially, what does
const obj **p
mean? 'p' is a variable of type pointer. It addresses a variable of type pointer which addresses a variable of type obj. I want to ensure that the function doesn't alter any of these. Can't I? What's the problem?
Posted
Updated 24-Aug-21 7:28am
v2
Comments
Richard MacCutchan 24-Aug-21 9:25am
   
You are trying to pass non-const objects to functions that you have defined to require const objects. Either change you function declarations or pass const objects.
ilostmyid2 24-Aug-21 9:34am
   
So why if the function accepts "const obj *" and I pass an "obj *" it doesn't issue a warning?
Richard MacCutchan 24-Aug-21 11:52am
   
Because gcc has already issued the warning. The const keyword is validated by the compiler, it is not known at run time.
Richard MacCutchan 24-Aug-21 12:04pm
   
Also, if you wish to comment on any of my comments then please use the Reply button above the message

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900