On 19/04/18 10:09, Manish Jain wrote:
> Hi all,
> 
> One of the historical artefacts of the C language has been the burden of 
> lugging around multiple declarations in a single statement, with some 
> well-known pitfalls:
> 
> int* ptr1, ptr2;
> 
> Since ptr2 looks like a pointer but actually is not, standard coding 
> guidelines recommend declaring like this:
> 
> int *p1, *p2;
> 
> If anything, this leads to bizarre statements - very misleading for 
> those trying to understand pointer usage in C or just read code:
> 
> int i;
> int *j = &i; // impression: *j is being assigned &i
> 
> char *k = "Text";  // impression: *k is "Text"
> 
> void *fx(char *z); // impression: *fx is will accept char & return void
> 
> 
> Each of these idiosyncrasies is best avoided by retaining the space 
> after the asterisk (and removing the one before) in a pointer 
> declaration. This really ought to be the standard coding guideline.
> 
> As for the problem of multiple declarations fraught in the suggestion 
> above, I would like gcc developers to please consider a compiler option 
> (--single-declarations perhaps) under which the programmer can only 
> introduce one declaration in one statement. If such an option could be 
> made available, it takes care of all declaration woes and lets declared 
> types bear close resemblance to what they appear to be from signatures.
> 
> Would my idea have takers on this list ?
> 

This is something that will cause a lot of mixed feelings.  Some people
see it as a very good thing that you can have multiple declarations of
connected types in the same declaration - others see it as a very bad
idea.  Certainly it is heavily used in existing code - making an option
to disable it would be impractical.

What I would suggest as a better compromise would be a warning, with
different levels:

-Wmulti-declaration=0 (the default):

        int a, b, c;            // OK
        int a, *b, c;           // OK
        int *a, *b, *c;         // OK
        int a[10], b[10];       // OK

-Wmulti-declaration=1 (Allow declarations of the same type only):

        int a, b, c;            // OK
        int a, *b, c;           // Warn
        int *a, *b, *c;         // OK
        int a[10], b[10];       // OK

-Wmulti-declaration=2 (Disallow multiple declarations)
        int a, b, c;            // Warn
        int a, *b, c;           // Warn
        int *a, *b, *c;         // Warn
        int a[10], b[10];       // Warn


A warning can always be turned into an error if you want.

Personally, I would use such a warning option at level 1, but not at
level 2.  I know others would use more or less.

(I have no idea if this is something that would be practical or easy to
implement in gcc, or if there is anyone who would be willing to do the
job.  I am just trying to refine the suggestion into something that
could have a wider appeal.)


Reply via email to