Thread: unable to pass values to struct as a byte.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    20

    unable to pass values to struct as a byte.

    Hi,

    I am using a struct and tying to send values to it as byte value

    Code:
    #include<stdio.h>
    
    
    typedef struct{
        unsigned r1:1;
        unsigned r2:1;
        unsigned r3:1;
        unsigned r4:1;
        unsigned r5:1;
        unsigned dummy:3; // empty port
    }rangeflags;
    
    
    rangeflags Range;
    
    
    int main()
    {
        Range.r1 = 1;
        printf("%d%d%d%d%d\n",Range.r1,Range.r2,Range.r3,Range.r4,Range.r5);
        Range = 0b00100000;
        //Range = 0x20;
        printf("%d%d%d%d%d\n",Range.r1,Range.r2,Range.r3,Range.r4,Range.r5);
    }
    Error:
    error: invalid suffix "b00100000" or incompatible types in assignment

    I am able to access the member as Range.r1 = 1; and have no problems.
    I want to send data whole at once, but how ?
    Last edited by raadys; 07-19-2014 at 12:20 AM. Reason: added code tags

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    Even I have updated to a union, i am able to access the member send data as hexa value. but not as a bit values.
    Code:
    #include<stdio.h>
    typedef unsigned char byte;
    typedef union _Range
    {
    	byte byte1;
    	struct{
    	unsigned r1:1;
    	unsigned r2:1;
    	unsigned r3:1;
    	unsigned r4:1;
    	unsigned r5:1;
    	unsigned dummy3:3;
    	};
    }rangeflags;
    
    
    rangeflags Range;
    
    
    int main()
    {
    	Range.r1 = 1;
    	printf("%d%d%d%d%d\n",Range.r1,Range.r2,Range.r3,Range.r4,Range.r5);
    	Range.byte1 = 0x10;
    	printf("%d%d%d%d%d\n",Range.r1,Range.r2,Range.r3,Range.r4,Range.r5);
    	Range.byte1 = 0b00000000;
    	printf("%d%d%d%d%d\n",Range.r1,Range.r2,Range.r3,Range.r4,Range.r5);
    }
    Error at line Range.byte1 = 0b00000000;
    error: invalid suffix "b00100000" or incompatible types in assignment

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Only compilers aimed specifically at the embedded microprocessor market recognise "0b" as indicating a binary constant. It is not a standard feature.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    thank u !

  5. #5
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Quote Originally Posted by Salem View Post
    Only compilers aimed specifically at the embedded microprocessor market recognise "0b" as indicating a binary constant. It is not a standard feature.
    TinyCC have such feature and compile x86 exe.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,271
    Quote Originally Posted by zub
    TinyCC have such feature and compile x86 exe.
    As does gcc, but the point remains that it is non-standard thus such implementations are compiler specific language extensions, hence the error that raadys encountered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Depending on what compiler you're using, it might also be helpful to include <stdint.h> and use uint8_t instead of unsigned char. There is no guarantee that char will be a single byte. On the other hand, there's no guarantee that uint8_t will exist either (systems where the smallest addressable bit of memory is 16-bits wide, etc.), but most systems that you're likely to encounter have an 8-bit type, and uint8_t will be supported. Note that this only works if your compiler supports the C99 or later standard(s).
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,271
    Quote Originally Posted by Elkvis
    There is no guarantee that char will be a single byte.
    It is guaranteed that char will be a single byte, but what isn't guaranteed is that the notion of a byte will correspond to the conventional notion of an 8-bit byte. On the other hand, CHAR_BIT is guaranteed to be at least 8, so there might not be a problem, though I'm not sure if alignment or something could be an issue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by laserlight View Post
    but what isn't guaranteed is that the notion of a byte will correspond to the conventional notion of an 8-bit byte.
    good point.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass enum values
    By Bargi in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2007, 02:55 AM
  2. pass multi-byte (or wide) characters to DeleteFile
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-30-2007, 12:56 AM
  3. Unable to calculate float values
    By CHurst in forum C Programming
    Replies: 2
    Last Post: 12-08-2005, 06:14 PM
  4. Switching byte values between variables
    By Chronom1 in forum C++ Programming
    Replies: 10
    Last Post: 02-23-2005, 12:18 PM

Tags for this Thread