Liste des Groupes | Revenir à l c |
On 08/04/2025 13:35, bart wrote:
Are they? So why does gcc report an error here (the two types are from my example):But this need not be the case. For example this is module A:The two types are entirely compatible.
>
--------------------------
#include <stdio.h>
>
typedef struct point {float a; float b;} Point;
>
float dist(Point);
>
int main(void) {
Point p = {3, 4};
printf("%f\n", dist(p));
}
--------------------------
>
And this is module B that defines 'dist':
>
>
--------------------------
#include <math.h>
>
typedef float length;
typedef struct _tag {length x, y;} vector;
>
length dist(vector p) {return sqrt(p.x*p.x + p.y*p.y);}
--------------------------
>
The types involved are somewhat different, but are compatible enough for it to work.
"typedef" does not introduce a new type, and struct types in different modules are compatible if they are build from the same compatible field types in the same order.Really? I could have added a dozen extra fields to one (or a pile of incompatible fields to both), and it can still work (especially if I pass the struct by reference).
>Not in C.
However, they could also be different enough (in a more elaborate example) for things to superficially work.
The preprocessor mechanisms available to work with source code are fairly easy to grasp (but may be complex in practice with multiple nested headers spread over a file system).
That's like complaining that integer addition is complex because someone might want to add 26 digit numbers. Stop being silly.You're missing the point: the concept may be simple, but as typically used in C, it is can be much more complex than necessary. Doing:
You really think that I wouldn't know how to do this if the language provided genuinely simple and well-thought-out and consistent rules?>That sounds very much like a "Bart" problem.
But I had, and do still have, difficulty with how exactly you import and export entities, even ones with linkage.
If you /genuinely/ want to know, and you can't figure it out with a quick google search, reading the page in the standards, or looking at an introduction to C book, then please ask. If you are just trying to claim opportunities to say it's all so difficult and confusing, then don't bother.Ah yes, the Microsoft approach: provide huge amounts of resources, manuals, training courses etc, rather than making something actually simple!
Which means a pile of badly written programs.>As a general rule, they have not.
How compilers deal with it have changed.
But it is true that some compilers have by default supported an extension that was designed to make interoperability with Fortran programs easier
And the workaround is...?However if I need to initialise the variable:Correct.
>
extern int table[]; // shared
int table[] = (10, 20, 30)
>
then other modules can't pick up length of the array.
>
The C standard gives clear rules here that make sense.What's the rational for allowing both static and extern versions of the same entity in the same file? And if that is allowed, why does the order matter?
mean they are the /only/ set of rules a language could have that makes sense, but they are not "ad hoc". That would imply that combinations like this could have unpredictable meanings.I don't mean 'ad hoc' to be 'indeterminate'. More like, 'mess', or 'free-for-all'.
Les messages affichés proviennent d'usenet.