Andrew Cottrell
Andrew Cottrell

Reputation: 3413

Can gcc display the pointer types when reporting "assignment from incompatible pointer type"?

Earlier today I was looking into this question: Why does one need to specify the row while assigning a pointer to a 2D Array? There was some confusion/disagreement regarding the types involved when attempting to assign the identifier of a multi-dimensional array to a pointer. Specifically, whether it would "decay" or "convert" (or whatever your preferred nomenclature may be, as there seems to be disagreement about that as well) to a char ** or a char ()[5] or a char (*)[5].

(Based on comments below, I'm including the code sample from the linked question here. Don't blame me, I didn't write it.)

int main()
{
  char onedstring[]={"1D Array"};
  char twodstring[][5]={"2D","Array"};
  char *p1,*p2;

  p1=onedstring;
  p2=twodstring;
  p2=twodstring[1];
}

I tested how my compiler reports the warning from the code in that question and got this result:
(Compiler: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4))

decay.c:15:7: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
     p2=twodstring;

It reports incompatible pointer types but does not report the types involved, which would be quite helpful in this instance. I haven't found a way (yet) to get that particular warning to also display the types. However, I have found that calling a function instead of assignment does result in a note that includes the types. Here is example code (and please note that it serves absolutely no purpose other than to generate a compiler warning):

void function_to_trigger_warning(char *c){}

int main(int argc, char **argv)
{
    char onedstring[]={"1D Array"};
    char twodstring[][5]={"2D","Array"};

    function_to_trigger_warning(onedstring);
    function_to_trigger_warning(twodstring);

    return 0;
}

And here is the warning output:

decay.c: In function ‘main’:
decay.c:20:33: warning: passing argument 1 of ‘function_to_trigger_warning’ from incompatible pointer type [-Wincompatible-pointer-types]
     function_to_trigger_warning(twodstring);
                                 ^
decay.c:7:6: note: expected ‘char *’ but argument is of type ‘char (*)[5]’
 void function_to_trigger_warning(char *c)
      ^

Looks like according to gcc the twodstring is treated as a char (*)[5] for the purposes of pointer assignment. That is valuable information!

Long story short (I know, too late): Is there a way in gcc to get similar output from other pointer assignment warnings?

Upvotes: 2

Views: 202

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 58172

Install a newer version of gcc. With gcc 8.1 or newer, your example yields the warning:

foo1.c: In function ‘main’:
foo1.c:8:5: warning: assignment to ‘char *’ from incompatible pointer type ‘char (*)[5]’ [-Wincompatible-pointer-types]
    8 |   p2=twodstring;
      |     ^

Try it on Compiler Explorer. (If necessary, click the "Output" button at the bottom of the compiler window to see the warning.)

Upvotes: 2

Related Questions