create new data type

This is a discussion on create new data type within the C Programming forums, part of the General Programming Boards category; hi. I want new data type. because it isn't 18 byte. help......

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    create new data type

    hi. I want new data type. because it isn't 18 byte. help...

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    typedef char newtype[18];
    You can now declare an instance:
    Code:
    newtype example="123456789012345678";
    Last edited by MK27; 05-16-2009 at 06:13 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    <sarcasm>create a struct with an 18-element array of chars</sarcasm>

    your question, well you didnt ask a question, your post is extremely vague. because of that you will get vague responses. try again, telling us your detailed question.

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Unless you have a 36-bit machine, you won't get an _integral_ type of exactly 18 bits. You can just approximate. Either you take 16 bits or 32. Or use a struct with a bitfield.

    Code:
    typedef struct {
        unsigned long bits : 18;
    } ustruct18;

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2
    I sorry. Frankly, I have 8 bits mikrocontroller. unsigned long is 32 bits and 4 byte. this is show much. I need 18 bits. I trying " Brafil " code. code is false. tanks...

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Put the "unsigned long bits : 18;" outside the struct to create to a 18 bit variable.
    Structs can be padded by the compiler and while you can disable it, it's compiler dependent.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by macroasm View Post
    I sorry. Frankly, I have 8 bits mikrocontroller. unsigned long is 32 bits and 4 byte. this is show much. I need 18 bits. I trying " Brafil " code. code is false. tanks...
    Not sure what you mean here; Brafil's code is letter-perfect. It creates a new type called "ustruct18" that you can use to declare variables, like so:
    Code:
    typedef struct {
        unsigned long bits : 18;
    } ustruct18;
    
    int main(void) {
    
        ustruct18 bob;
        bob.bits = 5;
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Then with following declaration in place
    Code:
    unsigned long bits : 18;
    
    Is the following assignment okay?
    bits = 3FFFF; //Will set all the 18 bits.

    Code:
    incase:
    
    if i need only three bits enable for the following statement
    
    unsigned int bit :5;
    
    Is this assignment correct if i have to set 3 bits?
    
    bit=0x07
    Thanks

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sanddune008 View Post
    Then with following declaration in place
    Code:
    unsigned long bits : 18;
    
    Is the following assignment okay?
    bits = 3FFFF; //Will set all the 18 bits.
    Thanks
    No. bits is a field of a variable. You have to have a variable of type ustruct18, then use the bits field of that variable.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    well that case

    Code:
    typedef struct {
        unsigned int type : 5;
        unsigned int bit:3;
    } ustruct;
    
    ustruct bye;
    
    Is this assignment correct if i have to set 3 bits?
    
    bye.bit=0x07;

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sanddune008 View Post
    well that case

    Code:
    typedef struct {
        unsigned int type : 5;
        unsigned int bit:3;
    } ustruct;
    
    ustruct bye;
    
    Is this assignment correct if i have to set 3 bits?
    
    bye.bit=0x07;
    It will set the least significant three bits, yes. If you wanted some other three bits set, you would have to use a different number.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    It will set the least significant three bits, yes. If you wanted some other three bits set, you would have to use a different number.
    Or the most significant 3 bits. Depends on the combination of compiler/processor architecture.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by matsp View Post
    Or the most significant 3 bits. Depends on the combination of compiler/processor architecture.

    --
    Mats
    Eh? I don't care what end they're on, they're the three bits that are worth the least.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you union your bits structure with an integer, you will find that setting those three bits will not necessarily set the least significant bits. It MAY do that, but by no means guaranteed. The only way to GUARANTEE bit-order is to use masks and shifts.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by matsp View Post
    And if you union your bits structure with an integer, you will find that setting those three bits will not necessarily set the least significant bits. It MAY do that, but by no means guaranteed. The only way to GUARANTEE bit-order is to use masks and shifts.

    --
    Mats
    Well, okay, they're the three bits that are worth the least that are accessible via the bitfield (which may or may not have any relationship with the underlying integer). If we (that is, the OP) care about the underlying integer/register then using a bitfield is the wrong thing to do (which is probably your point).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21