typedef array with const size?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: typedef array with const size?

  1. #1
    Join Date
    Oct 2008
    Posts
    45

    Unhappy typedef array with const size?

    can you typedef an array with constant size, I'm looking for something like:

    typedef char[4] char4;

    but this won't work and this too don't work:

    typedef [4]char char4;

    is there a way to declare a type of array of constant size, I want:

    char4 a; <=> char a[4];

    any help is greatly appriciated

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: typedef array with const size?

    typedef char char4[4];

    maybe?

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    Re: typedef array with const size?

    Out of curiosity though, but why do you want to do this?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Oct 2008
    Posts
    45

    Re: typedef array with const size?

    I want to create a dynamic array of char arrays of size 4, since this:
    char *a[4];
    doesn't work sice this creates an array of size 4 of char pointers :S

    I was thinking if this "char4" type def works I can do the following:
    char4 *a=new char4[n];

    and I didnt want to use a class to avoid using '.' every time I want to access something, like:
    a[i].array4

    well now I'm using classes out of nessecity

    any ideas?

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    Re: typedef array with const size?

    Well, I think Russco gave the correct syntax, but my concern is that arrays decay to pointers to their first element at every opportunity, so a char4 can easily decay to a char*. While it may be easy to see that a char[4] decays to a char*, it may be harder to spot that a char4 decays to a char*.

    Quote Originally Posted by compuKidd
    and I didnt want to use a class to avoid using '.' every time I want to access something, like:
    a[i].array4
    Personally, I feel that you should not avoid this as it could make your life easier in the long run. Of course, you should use more descriptive names in order to benefit more (char4 is certainly not very descriptive, and neither is array4).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Sep 2012
    Posts
    1

    Re: typedef array with const size?

    Quote Originally Posted by compuKidd View Post
    can you typedef an array with constant size, I'm looking for something like:

    typedef char[4] char4;

    but this won't work and this too don't work:

    typedef [4]char char4;

    is there a way to declare a type of array of constant size, I want:

    char4 a; <=> char a[4];

    any help is greatly appriciated

    any help is greatly appreciated. this is right not appriciated.

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: typedef array with const size?

    If you're using the latest C++ then you have this option.
    Code:
    typedef std::array<char, 4> char4;
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,970

    Re: typedef array with const size?

    "decomposing" a type into what is is can be tricky in C++. it can be even trickier to sometimes make the type you want.

    Code:
    // Type* Name[4]  creates an array of 4 times a Type*
    // In the case of chars that's a char* or also commonly referred to as a C-string.  You can declare and initialise it as such:
    char* CP4[4] = {"1", "22", "333", "4444"};
    
    // What you wanted is:
    // Type (*Name)[4]  this creates a pointer to an array of 4 times a Type.
    // Use as such:
    char c[4] = {1,2,3,4};
    char (*PC4)[4] = &c;   // Or via allocation...

    It's a somewhat rare type of construct, but I have needed to use it like that in a couple projects. Knowing how things work is handy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center