declaring data type

This is a discussion on declaring data type within the C Programming forums, part of the General Programming Boards category; How to declare a variable length data type. It may be of any type (char, int, float). for eg: In ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    39

    declaring data type

    How to declare a variable length data type. It may be of any type (char, int, float).

    for eg:
    In linux,
    int has 4 bytes
    char has 2 bytes

    like this i am having one variable which is of variable length. how to declare it.

    thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Generally, you use typedefs, e.g.
    Code:
    typedef int int32; // Int is 4 bytes = 32 bits. 
    typedef char int8; // Char is 1 byte -> 8 bits. 
    typedef short int16; // Short is 2 bytes -> 16 bits.
    You may have to make #ifdef's to match up some types to certain compilers/processors.

    --
    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.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    39
    I think u didn't understand my problem clearly. What i want is i am having a variable of name 'X', if i declare X as an int type. it will be of size 4 bytes. I dont know the type of the variable, it may be of any type and of any length. how to declare it.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    I think the best solution is to tell us what you're trying to do.
    You can't just define a variable of any type to be an undefined amount of bytes.
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure if this is what you are asking for either, but the only type that can be an entirely arbitrary length is a char array - and to make it compile-time, you would need to allocate the memory, using a char pointer to hold the address of your allocation (and a size_t variable to hold it's size, perhaps?).

    Technically, you can also use a void pointer to describe an arbitrary size data blob, but again, it's requiring memory allocation and void pointers are just a way to hold a memory address, you can not (directly) access the data held in such a pointer.

    As elysia says, maybe you can describe what you want to do, then we can perhaps describe the best way to do what you want.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM

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