That does not make much sense. It looks like you are trying to do two things at once. Do you have a compiler that will swallow this construct?Does anyone here ever use, or advocate this kind of contrivance:
See here. I'm asking mainly about the concept of a typedef being an array.Code:typedef struct { int age; int height; } Person[3];
What's the problem with it?Does anyone here ever use, or advocate this kind of contrivance:
See here. I'm asking mainly about the concept of a typedef being an array.Code:typedef struct { int age; int height; } Person[3];
typedef double Complex[2];
typedef double Coord3D[3];
The question has come up as a matter of language design, I'm simply looking at what opinions come up, not sure if I should adopt the concept or not, in a new language I'm working on.What's the problem with it?
Is it the ability to typedef something as an array of structures that is bothering you?
Does the ability to typedef something, say a double, as an array also cause heartache?
For instance, being able to typedef a complex number as a 2D array of doubles, or typedef a position in space as a 3D array of doubles?
Code:typedef double Complex[2]; typedef double Coord3D[3];
To a certain degree, it depends on how orthogonal you want your language to be.The question has come up as a matter of language design, I'm simply looking at what opinions come up, not sure if I should adopt the concept or not, in a new language I'm working on.
uint8_t[4] counters;
Actually, the fact that you can't do that it what lends consistency to it.Also it's syntax is odd, it has an array bounds specifier after the type name but ordinarily in C we put the bounds specifier after the name of the variable we're declaring.
We can't do this for example
So it seems inconsistent.Code:uint8_t[4] counters;
Well you raise some reasonable points I guess. But look at this again:Actually, the fact that you can't do that it what lends consistency to it.
Consider:
typedef double MYARRAY[4];
MYARRAY myarray1, myarray2;
The 'MYARRAY' is replaced by everything in the typedef statement between the typedef and the name of the new type. Everything after the name of the new type is appended to each declared variable.
So this becomes:
double myarray1[4], myarray2[4];
This is completely consistent with the simpler use case:
typedef double MYDOUBLE;
MYDOUBLE mydouble1, mydouble2;
becomes
double mydouble1, mydouble2;
If you want some inconsistency, look at typedefs of pointers.
typedef double* MYDBLPTR;
MYDBLPTR dblptr1, dblptr2;
This does NOT become
double* dblptr1, dblptr2;
but rather
double *dbltptr1, *dbltptr2;
The usual handwaving that is done to make this go away is to claim that typedefs are applied independently to each variable that is declared as if they were on separate lines.
typedef struct {
int age;
int height;
} Person[3];
But why would you want to?Well you raise some reasonable points I guess. But look at this again:
How does one declare a single instance of the underlying struct? or an array of two of them? One could argue "well that's the prerogative of the developer" but the problem vanishes if we can never create "array typedefs" in the first place. We'd be forced to only ever be able to define single struct types and thereafter declare them as arrays.Code:typedef struct { int age; int height; } Person[3];
typedef struct {
double x;
double y;
} Triangle[3];
Well as to "But why would you want to?" Well I might be asked to, told to! I might have a new problem, a change to make to the code that is best made by me creating an array of five 'Person' structs. The fact is I cannot do so, the language makes it hard.But why would you want to?
The whole idea is you are creating a new data type, in this case Person, that is defined to be an array of three of those structures. That's all you are doing. You are NOT defining a structure of Person and then typedef'ing it to be an array of three of those.
The example is a poor one because it has no intuitive use case.
But again consider the example I gave of typedef'ing a 3D point. How does one declare a single instance of the underlying double? Or an array of two of them? You DON'T! The typedef defined an new type that consists of an array of three doubles. Period.
Perhaps a better example might have been something like:
Personally, I would have broken this apart and defined the underlying structure as it's own entity, say POINT, and then used that to build up a TRIANGLE type. But that is because it's natural to view a point as being a more generic entity that can be used to build up more things than just triangles. But I can easily envision that cases exist in which the underlying structure doesn't have a more general use and the object being defined simply consists of a fixed size number of them.Code:typedef struct { double x; double y; } Triangle[3];
typedef struct {
double x;
double y;
} Coordinate2D;
typedef struct {
Coordinate2D points[3];
} Triangle;
typedef struct
Coordinate2D points[4];
} Square;
If you want to do it this way, then do it this way.Now regarding your triangle example, it can still be elegantly coded, if the ability to create "array typedefs" did not exist it would be easy to do what you want still, you'd just do it differently:
etc.Code:typedef struct { double x; double y; } Coordinate2D; typedef struct { Coordinate2D points[3]; } Triangle; typedef struct Coordinate2D points[4]; } Square;
No Sir, with all due respect I did not say that at all.If you want to do it this way, then do it this way.
What you are basically saying is that your idea of "good" language design is that you force everyone to do everything exactly the one way that YOU think it should be done and that it is simply not possible that any one using your language could possibly have any reason for ever wanting to do it any other way than the one way that you have decreed that it must be done.
I have challenged you to demonstrate any material, tangible negative consequence that would arise if C did not permit this specific capability.The same could be said for C having three kinds of loops when there is nothing that can't readily be done with just a while() loop (or any of the three, for that matter).
If you feel it is pointless, then don't include it in your language. But, in doing so, at least acknowledge that you ARE forcing people to conform to how YOU think they must write their code.
There's nothing intrinsically wrong with this -- and every language has to make those same decisions.
There are philosophical advantages to only being able to do things one way in a language. But there are also advantages to being able to do things in different ways to give the programmer options in deciding what constructs best match their logic or their approach to a problem. It's a balancing act.
By the way, shouldn't your signature text read:I've already mentioned that by not allowing it, you decrease the orthogonality of the language. Whether that is a negative depends on how valuable you believe orthogonality to be. I am not going to spend a bunch of my time trying to imagine every possible way that every programmer might choose to use a particular construct searching for one that would somehow satisfy you that not being able to do it the way that they choice would constitute some material, negative consequence. If you don't want to allow it in your language, then don't allow it.
"There are 11 types of people in the world: those who understand binary, those who don't, and those who can work in any number base".
The only people that think that are those that can't work in any number base.By the way, shouldn't your signature text read:
void get_date (date_ptr);