extern typedefed struct in linked file how?
Author |
Message |
je.. #1 / 25
|
 extern typedefed struct in linked file how?
Hi, please enlighten me if you can. I have something like: ---File1.c // Outside of main typedef struct { int whatever; int whatever2; } MyType; ---File2.c extern MyType; int some_function() { MyType* MyVar; // Error from gcc when compiling (not linking yet) on this line, "MyVar undeclared" return 0; Quote: }
Whats the proper way to declare my MyType structure for use in this file? thanks i'm stumped!
|
Sun, 27 Jun 2004 10:43:10 GMT |
|
 |
Joe Wrigh #2 / 25
|
 extern typedefed struct in linked file how?
Quote:
> Hi, please enlighten me if you can. > I have something like: > ---File1.c > // Outside of main > typedef struct { > int whatever; > int whatever2; > } MyType; > ---File2.c > extern MyType; > int some_function() > { > MyType* MyVar; // Error from gcc when compiling (not linking > yet) on this line, "MyVar undeclared" > return 0; > } > Whats the proper way to declare my MyType structure for use in this > file? > thanks i'm stumped!
Use a header file "myhdr.h" to declare the typedef and #include it in both .c files. --
"Everything should be made as simple as possible, but not simpler." --- Albert Einstein ---
|
Sun, 27 Jun 2004 11:20:34 GMT |
|
 |
CBFalcone #3 / 25
|
 extern typedefed struct in linked file how?
Quote:
> Hi, please enlighten me if you can. > I have something like: > ---File1.c > // Outside of main > typedef struct { > int whatever; > int whatever2; > } MyType;
^^^^^^ This describes a type, not an object. It applies to this file. MyType athing; /* is an instance of a MyType */ Quote: > ---File2.c > extern MyType;
so what? Never heard of it. Where is the #include that ports the definition of MyType? Then you could add extern MyType athing; /* and it might get found */ Quote: > int some_function() > { > MyType* MyVar; // Error from gcc when compiling (not linking > yet) on this line, "MyVar undeclared"
This line "yet) ...." is a syntax error. Or did you intend to write proper line wrap safe comments enclosed in /* .. */ ? Regardless, your line far exceeded the 80 char max for newsgroups, which should realistically be kept under 72. Quote: > return 0; > } > Whats the proper way to declare my MyType structure for use in this > file? > thanks i'm stumped!
--
Available for consulting/temporary embedded and systems. (Remove "XXXX" from reply address. yahoo works unmodified)
|
Sun, 27 Jun 2004 12:57:24 GMT |
|
 |
Ra #4 / 25
|
 extern typedefed struct in linked file how?
typedef defines a new type, it does not create a variable for you! for instance: typedef struct{ /* <- unnamed struct */ int a; ... Quote: }MyStruct; /* now named to MyStruct */
now, the MyStruct is similar to any other type, like say, int. you have to declare a var to use it. say something like... MyStruct var1; ... Later Raj
|
Sun, 27 Jun 2004 18:54:20 GMT |
|
 |
Richard Heathfiel #5 / 25
|
 extern typedefed struct in linked file how?
Quote:
> typedef defines a new type, it does not create a variable for you!
No, typedef does not define a new type. It defines an alias for an existing type. Quote: > for instance: > typedef struct{ /* <- unnamed struct */ > int a; > ... > }MyStruct; /* now named to MyStruct */
In this example, *first* you define a new type, and then you define a new alias for it. --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Sun, 27 Jun 2004 22:57:35 GMT |
|
 |
Eric Go #6 / 25
|
 extern typedefed struct in linked file how?
Quote:
> No, typedef does not define a new type. It defines an alias for an > existing type.
No, it does not. The C99 standard states: "A typedef declaration does not introduce a new type, only a synonym for the type so specified." No where in section 6.7.7, where typedef is defined, is 'alias' used to describe what a typedef is. Furthermore, 'alias' and 'synonym' have different enough meanings such that they don't appear together in the Thesaurus' I checked. -- == Eric Gorr ===== http://www.*-*-*.com/ :9293199 === "Therefore the considerations of the intelligent always include both benefit and harm." - Sun Tzu == Insults, like {*filter*}, are the last refuge of the incompetent... ===
|
Mon, 28 Jun 2004 00:53:49 GMT |
|
 |
Richard Heathfiel #7 / 25
|
 extern typedefed struct in linked file how?
Quote:
> > No, typedef does not define a new type. It defines an alias for an > > existing type. > No, it does not.
<shrug> Yes, it does. Quote: > The C99 standard states: > "A typedef declaration does not introduce a new type, only a > synonym for the type so specified."
Yes. Quote: > No where in section 6.7.7, where typedef is defined, is 'alias' used to > describe what a typedef is. Furthermore, 'alias' and 'synonym' have > different enough meanings such that they don't appear together in the > Thesaurus' I checked.
Take it to alt.usage.english please. It is commonplace in comp.lang.c for the word "alias" (and its relative, "aliasing") to be used in this way. --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Mon, 28 Jun 2004 02:12:05 GMT |
|
 |
Chris Tore #8 / 25
|
 extern typedefed struct in linked file how?
Quote:
>The C99 standard states: > "A typedef declaration does not introduce a new type, only a > synonym for the type so specified." >No where in section 6.7.7, where typedef is defined, is 'alias' used to >describe what a typedef is. Furthermore, 'alias' and 'synonym' have >different enough meanings such that they don't appear together in the >Thesaurus' I checked.
The words are different parts of speech -- "alias" is an adverb, while "synonym" is a noun. But "alias" does have a noun form; here is what the online Miriam-Webster dictionary (http://www.m-w.com) has to say about that: Main Entry: 2alias Function: noun Date: 1605 : an assumed or additional name Compare that with the definition of "synonym": Date: 15th century 1: one of two or more words or expressions of the same language that have the same or nearly the same meaning in some or all senses 2: a symbolic or figurative name : METONYM 3: a taxonomic name rejected as being incorrectly applied or incorrect in form -- compare HOMONYM Given that what typedef does is to create an *additional* name for some existing type, I submit that the Standard has mis-used the English word "synonym" here. Dictionary definitions 2 and 3 do not seem to apply, and dictionary definition 1 gives "synonym" far too much latitude -- for instance, if INT_MAX and LONG_MAX are the same value, and INT_MIN and LONG_MIN are the same value, it would be correct English to call int and long "synonyms": "two or more words ... of the same language ... that have ... nearly the same meaning in some ... senses." Of course, one can argue that "alias" is pejorative. Although m-w.com does not show it above, the word "alias" often implies a degree of dishonesty. It also is clearly intended for anthropomorphic entities[%]. The thesaurus I liked enough to buy ("The Synonym Finder" by J. I. Rodale, Rodale Press) suggests these for the noun form of "alias": pseudonym, anonym, false name; assumed name, stage name, pen name, nom de plum, /Fr. nom de guerre/, allonym; nickname, sobriquet. Yet I would also like to quote the first paragraph of the Introduction to this book, by Laurence Urdang (28 July 1978): Those who work with language know that there is no such thing as a true ``synonym.'' Even though the meanings of two words may be the same -- or nearly so -- there are three characteristics of words that almost never coincide: frequency, distribution, and connotation. /Panthera leo/ and /lion/, /cucurbit/ and /squash/, /sodium carbonate/ and /washing soda/ have quite different frequencies in English. We all know that a /house/ is not a /home/, that not all /women/ are /ladies/, that not all /men/ are /gentlemen/; at a more subtle level, we soon learn the differences between /motherly/ and /maternal/, /fatherly/ and /paternal/, /brotherly/ and /fraternal/. These are connotative differences. In C programming, however, typedef names are *exact* copies of the original "true" name of the type. All differences are syntactic, even including the fact that qualifiers never "penetrate" a typedef (so that "typedef char *T; const T b" does not mean the same thing as "const char *b" but rather "char *const b"). Again, then, the subtle nuances of "synonym" do not apply to typedef, while those of "alias" do. Aside from its slightly wicked connotation (of which I actually approve: typedefs *are* slightly wicked :-) ), its only real fault is that it is meant to apply to humans. [%footnote: "Don't anthropomorphize computers; they hate that." --anon.] -- In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
|
Mon, 28 Jun 2004 06:08:26 GMT |
|
 |
Eric Go #9 / 25
|
 extern typedefed struct in linked file how?
Quote:
> It is commonplace in comp.lang.c > for the word "alias" (and its relative, "aliasing") to be used in this > way.
Terminology used by the standard, especially when the standard is clear on a topic such as this, should be preferred at all times. Since the word 'alias' and 'synonym' are not interchangeable as determined by any respected Thesaurus or Dictionary, the potential for deception is simply to great especially among those who may not be aware of what is and is not in common, but inaccurate, usage in this newsgroup. -- == Eric Gorr ===== http://www.*-*-*.com/ :9293199 === "Therefore the considerations of the intelligent always include both benefit and harm." - Sun Tzu == Insults, like {*filter*}, are the last refuge of the incompetent... ===
|
Mon, 28 Jun 2004 07:14:57 GMT |
|
 |
Eric Go #10 / 25
|
 extern typedefed struct in linked file how?
Quote:
> The thesaurus I liked enough to buy ("The Synonym > Finder" by J. I. Rodale, Rodale Press) suggests these for the noun > form of "alias": > pseudonym, anonym, false name; assumed name, stage name, pen > name, nom de plum, /Fr. nom de guerre/, allonym; nickname, > sobriquet.
I do not see the word 'synonym' in that list.
|
Mon, 28 Jun 2004 09:20:59 GMT |
|
 |
Hatem Al-Nagga #11 / 25
|
 extern typedefed struct in linked file how?
typedef struct { int whatever; int whatever2; Quote: } MyType;
This makes MyType a "type" not a variable You can only use the extern keyword with variables Foe example the following is legal In the first module: MyType var; And in the second: extern MyType var; But "extern MyType" is illegal The solution for your problem is to put the structure and declaration in both modules or you had better make a header file and put the structure in it
|
Sun, 27 Jun 2004 12:09:00 GMT |
|
 |
Ra #12 / 25
|
 extern typedefed struct in linked file how?
Quote: > typedef defines a new type...
Sorry folks, I agree I should have better said it as: typedef introduces an alternate name for an existing type. This is commonly used to avoid typing the word "struct" before struct vars. My mistake ;-) Raj
|
Mon, 28 Jun 2004 13:37:48 GMT |
|
 |
Chris Tore #13 / 25
|
 extern typedefed struct in linked file how?
Quote:
>> The thesaurus I liked enough to buy ("The Synonym >> Finder" by J. I. Rodale, Rodale Press) suggests these for the noun >> form of "alias": >> pseudonym, anonym, false name; assumed name, stage name, pen >> name, nom de plum, /Fr. nom de guerre/, allonym; nickname, >> sobriquet.
Quote:
>I do not see the word 'synonym' in that list.
Perhaps this is because it is not there.
Given that what typedef does is to create an *additional* name for some existing type, I submit that the Standard has mis-used the English word "synonym" here. Now, Standards *do* tend to introduce their own jargon, redefining various words to mean something new. The C standard's version of "object" is of course quite different from the dictionary definitions: 1 a: something material that may be perceived by the senses <I see an object in the distance> b : something that when viewed stirs a particular emotion (as pity) <look to the tragic loading of this bed... the object poisons sight; let it be hid -- Shakespeare> 2: something mental or physical toward which thought, feeling, or action is directed <an object for study> <the object of my affection> <delicately carved art objects> 3 a: the goal or end of an effort or activity : PURPOSE, OBJECTIVE <their object is to investigate the matter thoroughly> b : a cause for attention or concern <money is no object> 4 : a thing that forms an element of or constitutes the subject matter of an investigation or science 5 a : a noun or noun equivalent (as a pronoun, gerund, or clause) denoting the goal or result of the action of a verb b : a noun or noun equivalent in a prepositional phrase synonym see INTENTION None of these is much like the Standard's definition: 3.15 Object [#1] A region of data storage in the execution environment, the contents of which can represent values. Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined. When referenced, an object may be interpreted as having a particular type; see 6.2.2.1. Thus it is not beyond the pale for the C standard to redefine the English word "synonym" to mean "alias", instead of simply using the word "alias". I, however, prefer to use this existing English word that, in the minds of most of those whom I would consider sensible, thoughtful, and intelligent readers, will conjure up the most appropriate image, both via denotation and connotation. As such, I will continue to use the word "alias", and encourage others to do likewise, but also encourage them (and attempt myself) to note, as needed, that this "alias" is what the Standard calls, in highly misleading fashion, a "synonym". If the reader finds that the word "alias" does *not* call forth an appropriate metaphor, that does not prove that the reader is not sensible, thoughtful, or intelligent, because after all, everyone is different. Still, communication requires some degree of shared world-view, and effort on both the readers' and writer's parts, and I believe that on the whole, the writer (singular) should put forth the most effort in this transaction. If you find my words misleading, confusing, or inappropriate, you should feel free to disagree, whether silently or (as it were) in print: in this newsgroup, at your choice; and if enough people -- where "enough" is strictly in my own judgement, of course -- write and say that my long flowery sentence structure is an impediment to communication, then I may choose to change. ("The sun set. The red rays bathed the small house. Inside, the man sat at the keyboard. His fingers were rough, calloused. He punched keys, making words. The words ran up the screen, like a river swollen from rain. `Do you understand?' he asked. The computer gave no answer.") -- In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
|
Mon, 28 Jun 2004 17:14:55 GMT |
|
 |
Richard Heathfiel #14 / 25
|
 extern typedefed struct in linked file how?
Quote:
> > It is commonplace in comp.lang.c > > for the word "alias" (and its relative, "aliasing") to be used in this > > way. > Terminology used by the standard, especially when the standard is clear > on a topic such as this, should be preferred at all times.
See Chris Torek's excellent reply elsethread (which, by your reply to it, you appear to have misunderstood). Quote: > Since the word 'alias' and 'synonym' are not interchangeable as > determined by any respected Thesaurus or Dictionary,
Interesting cop-out. If I /were/ to come up with a counter-example, you could then simply retort that it's not "respected". Quote: > the potential for > deception is simply to great especially among those who may not be aware > of what is and is not in common, but inaccurate, usage in this > newsgroup.
There is no potential *at all* for deception. The usage of "alias" to describe the behaviour of typedef is sufficiently accurate that it is in common use by regular members of this newsgroup, who are some of the most pedantic people I've ever encountered. Dictionaries can be very helpful when you encounter a word in normal use and don't happen to know what it means, but they make very bad legal weaponry, especially in a technical discussion. One should not /rely/ on dictionary definitions for technical terms (look up 'padding' in your favourite non-technical dictionary if you don't believe that - 'padding' is used by the Standard, but not defined therein AFAICT, so it's a valid illustration of my point), since those terms are very frequently the result of the grafting of new meanings onto old words, a process which is happening at a much greater rate than a non-technical dictionary can possibly match. In this case, IMHO, actual usage is a much better guide. --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Mon, 28 Jun 2004 18:15:43 GMT |
|
 |
Eric Go #15 / 25
|
 extern typedefed struct in linked file how?
Quote:
> Given that what typedef does is to create an *additional* name for > some existing type, I submit that the Standard has mis-used the > English word "synonym" here.
Well, then you are welcome to take it up with the standards committee. If they decide to change the wording, then I will accept 'alias' as an accurate word in relation to typedef. Until such a time, it is simply inaccurate to use 'alias'. It is irrelevant that it is in common usage among the regulars. This group should be as much about the regulars communicating with one another as it is about them communicating with people who ask questions. To redefine what a typedef creates a potential deceptive situation, which is trivially avoided. -- == Eric Gorr ===== http://www.*-*-*.com/ :9293199 === "Therefore the considerations of the intelligent always include both benefit and harm." - Sun Tzu == Insults, like {*filter*}, are the last refuge of the incompetent... ===
|
Mon, 28 Jun 2004 23:34:41 GMT |
|
|
Page 1 of 2
|
[ 25 post ] |
|
Go to page:
[1]
[2] |
|