-
November 29th, 2008, 10:39 AM
#1
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
-
November 29th, 2008, 10:53 AM
#2
Re: typedef array with const size?
typedef char char4[4];
maybe?
-
November 29th, 2008, 11:29 AM
#3
Re: typedef array with const size?
Out of curiosity though, but why do you want to do this?
-
November 29th, 2008, 02:04 PM
#4
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?
-
November 29th, 2008, 02:17 PM
#5
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*.
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).
-
September 12th, 2012, 09:59 PM
#6
Re: typedef array with const size?
Originally Posted by compuKidd
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.
-
September 13th, 2012, 03:45 AM
#7
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
-
September 13th, 2012, 10:49 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|