I believe sm & 1U could and should silence the warning. Please, open a
bug report http://gcc.gnu.org/bugs.html and add manu@xxxxxxxxxxx to the
CC list. We probably have a similar issue with
unsigned short x = sm & 1U; or does that work?
Nevertheless, it seems casting types like (unsigned int:1)sm would be a
useful feature of C. Is it contemplated for some future version of the
standard?
Thanks,
Manuel.
Christian Iseli wrote:
Dear all,
Given:
---- test.c ----
typedef struct _my_struct_t {
unsigned int small:1;
unsigned int big:31;
} my_struct_t, *my_struct_p_t;
void
my_func(unsigned int sm, unsigned int bi, my_struct_p_t msp)
{
msp->small = sm;
msp->big = bi;
}
----
and:
gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-cpu=generic --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.3.0 20080229 (Red Hat 4.3.0-0.13) (GCC)
and:
gcc -O2 -std=gnu99 -W -Wall -Wconversion -pedantic -c test.c
I get:
test.c: In function ‘my_func’:
test.c:9: warning: conversion to ‘unsigned char:1’ from ‘unsigned int’ may alter its value
test.c:10: warning: conversion to ‘unsigned int:31’ from ‘unsigned int’ may alter its value
I can't seem to find a typecast incantation that would tell GCC that I
did indeed take proper precautions and that the assignment is fine.
I also tried "msp->small = sm & 1U;" but it doesn't help
Any hints ?
Thanks,
Christian