Re: p124 K&R



On Sun, 3 Aug 2008 15:44:02 -0700 (PDT), mdh <mdeh@xxxxxxxxxxx> wrote:

Hi All,
I have a couple of questions regarding the discussions on page 124 of
k&r.

1)

This references functions working with complicated declarations in C.

before main(...,...){}, (in file main.c), an enum is defined thus.

enum {NAME, PARENS, BRACKETS};

int gettoken(){......}


refers to this with a return line thus.

return tokentype = PARENS;

Now, I am assuming that gettoken and main are compiled in the same
file?

As shown on pages 124 and 125, they must be.


I am trying to compile "gettoken" in my "foo.c" file. Hence I wish to
ref the enum from there.

In addition to the enum, you also need token, tokentype, string.h, and
ctype.h.


I have tried this.

Giving the enum an identifier,

"enum syntax {NAME, PARENS, BRACKETS};"

and then declaring it in foo.c thus.

extern enum syntax; but this gives a warning "useless storage class
specifier in empty declaration "

Even if this did not generate a diagnostic, while compiling foo.c what
would the compiler know about enum syntax? How would it know that
NAME was a synonym for 0, PARENS for 1, etc?




2) staying with the "extern" theme.

A char array is defined in main.c

" char token[10]; "



Again referenced from foo.c using syntax "extern char token".

This in of itself does not give a warning, ( which I think is
expected) but this line does.

char *p = token " initialization makes pointer from integer without a
cast"

I would have thought that 'token' is changed to a ptr to char of the
first element in the array, so not quite sure what or if to try and
correct this.

Your extern declaration did not say token was an array. It said token
was a char. If you had coded
extern char token[];
your initialization of p would have worked.


Thanks in advance....and holding my head...there have been some testy
responses lately...so hopefully I do not fit those stereoptypes!!

If you want to split the main and gettoken functions into different
source files (technically translation units), you have to insure that
every file scope declaration in main.c is visible in foo.c AND that
every file scope object in main.c is declared as extern in foo.c.

One way to do this is copy all the lines on page 124 from
#include <stdio.h>
through
int gettoken(void);
from main.c to foo.c. Then copy the lines from
int tokentype;
through
char out[1000];
and insert the storage class "extern" in front of each.

I don't like this because 1) any change in main.c must be duplicated
in foo.c and 2) the compiler cannot check it the two are consistent.

My preferred approach is MOVE the lines from
#include <stdio.h>
through
int gettoken(void);
to foo.h. Then COPY the lines from
int tokentype;
through
char out[1000];
and insert the storage class "extern" in front of each. Add
#include "foo.h"
to the front of both main.c and foo.c. Changes to the various
declarations need only appear in foo.h. During the compilation of
main.c, any inconsistencies between foo.h and the file scope objects
defined in main will be flagged.

--
Remove del for email
.



Relevant Pages

  • Re: p124 K&R
    ... ref the enum from there. ... Your extern declaration did not say token was an array. ... every file scope object in main.c is declared as extern in foo.c. ...
    (comp.lang.c)
  • Re: jni problem linking into existing s.o. lib with externs
    ... and declared separately (with extern, ... in a header file). ... BOB.h: extern char *ERRMSG ... you include BOB.h to see the declaration. ...
    (comp.lang.java.programmer)
  • Re: MFC dll and exe porting to 64-bit
    ... Why the obsolete 'char *' instead of the proper LPTSTR? ... We are no longer programming 16-bit windows in an 8-bit character world, ... string arguments should probably be LPCTSTR, that is, const parameters. ... declaration of the function itself, in context (meaning show the surrounding class ...
    (microsoft.public.vc.mfc)
  • Re: keyword extern
    ... >> definition or declaration until the end of the translation unit being ... >> One never needs to use the extern keyword with a function definition, ... >> ...then here is how a C compiler understands them. ... If any code in the source file access 'x', ...
    (comp.lang.c)
  • Re: "extern" inside a block
    ... In multi source file programs, ... > I know the behaviour when extern declaration follows a non-extern ... Linkage of Identifiers ... scope in which a prior declaration of that identifier is visible [and ...
    (comp.lang.c)