NO

Author Topic: Invalid error message when using compound literals and const pointers  (Read 2170 times)

neo313

  • Guest
Assigning an compound literal to a const void pointer( const void* ), gives a completely bogus warning: error #2139: Too many initializers.

The constraints are somewhat specific. The pointer must be const void* and the assignment must be a compound literal of any type.

Here are some examples of what gives an error and what doesn't. Of course all of them are valid C code.
Code: [Select]
#include <stdio.h>

void Test( const void* f )
{
printf( "%p\n" , f ) ;
}

struct holdvoid
{
const void* h ;

} ;

int main( void )
{

//////WORKS/////

int n = 123 ;
Test( &n ) ;

const int m = 123 ;
Test( &m ) ;

n = ( int ){ 123 } ;
Test( &n ) ;

int* np = &( int ){ 123 } ;
Test( np ) ;

const int* cnp = &( const int ){ 123 } ;
Test( cnp ) ;

void* vp = &( int ){ 123 } ;
vp = &( int ){ n } ;
( void )vp ;

const void* cvpw = &n ;
( void )cvpw ;

//////DOESN'T WORK/////

const void* vpp = &( int ){ 123 } ;
( void )vpp ;

const void* cvp = &( const int ){ 123 } ;
( void )cvp ;

Test( &( int ){ 123 } ) ;

Test( &( const int ){ 123 } ) ;

( void )( struct holdvoid ){ &( const int ){ 123 } } ;

return 0 ;
}

http://ideone.com/xIoEv5

I have noticed that if the initializer is a variable, the bug is not present. If I replace the constants with variables in the it doesn't work part of the code:

Code: [Select]
const void* vpp = &( int ){ n } ;
( void )vpp ;

const void* cvp = &( const int ){ n } ;
( void )cvp ;

Test( &( int ){ n } ) ;

Test( &( const int ){ n } ) ;

( void )( struct holdvoid ){ &( const int ){ n } } ;

The code compiles.
« Last Edit: October 07, 2014, 11:38:37 PM by neo313 »