typedef and array

This is a discussion on typedef and array within the C Programming forums, part of the General Programming Boards category; Ok, say I have the following: Code: typedef enum {Void,Integer,Boolean,Char, Null} Type; and I want a string to have a ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    typedef and array

    Ok, say I have the following:

    Code:
    typedef enum {Void,Integer,Boolean,Char, Null} Type;
    and I want a string to have a type as an array of char, so how can I do so?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do
    char[26] string;

    You'll need to add that into your typedef, I guess; or maybe "Void" is supposed to represented "void *", which this can be cast into.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    ok, so I guess I should add an array of char here inside the typedef as well, am I right?

    so I would have another type that says:

    char[Integer]. so the typedef now will be:

    Code:
    typedef enum {Void,Integer,Boolean,Char, Null, Char[Integer]} Type;

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh and one more off topic question, in C an int is compatible with char and vice versa, I don't really get this concept how is an int suppose to be compatible with a char and vice versa? can you give me an example?

    what I have in head when I read this statement is the following would be valid:

    int c = 'a'

    but I don't think that's what it meant...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course that's valid -- but maybe not for the reason you expect. You probably expect 'a' to be of char type. But, in C, that's not true; 'a' is an int constant. (In C++ it is a char constant.)

    But what it means to be compatible is that char, like short, long, and long long, are all integral types (they store integers). So when passing chars to printf, or doing arithmetic on chars, they get "promoted" to ints, temporarily.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and then so doing char b = 12;

    is also valid in c?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You bet. (C++ too.)

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so something like:

    int test[200];

    test[2] = 'a'

    or

    char foo[200];

    foo['a'] = 55;

    are both valid?

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes...

    Try it out for yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of typedef struct
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 10-25-2008, 02:33 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Typedef array variable
    By MWAAAHAAA in forum C Programming
    Replies: 1
    Last Post: 12-24-2006, 06:43 AM
  5. typedef for pointer to array of char
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2003, 03:21 PM

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