473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

p124 K&R

mdh
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?

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

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 "

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.

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


Aug 3 '08 #1
5 1616
mdh
On Aug 3, 3:44*pm, mdh <m...@comcast.netwrote:
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?

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

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 "

Please ignore Q2...figured it out.
>

Thanks in advance....and holding my head...there have been some testy
responses lately...so hopefully I do not fit those stereoptypes!!
Aug 4 '08 #2
On Sun, 3 Aug 2008 15:44:02 -0700 (PDT), mdh <md**@comcast.netwrote:
>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
Aug 4 '08 #3
mdh
On Aug 3, 6:30*pm, Barry Schwarz <schwa...@dqel.comwrote:
>
This references functions working with complicated declarations in C.
before *main
enum {NAME, PARENS, BRACKETS};


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.
In reality, my "foo.c" and "foo.h" contain all the little functions I
have written as I have moved through k&r. This has helped me in
understanding C. ( So I have str_cmp, str_len etc etc and if I need a
new one, I have written it)...but, perhaps the time has come to move
on and use those libraries.

>
I have tried this.
Giving the enum an identifier,
"enum syntax {NAME, PARENS, BRACKETS};"

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?

Well, that is what I find a little confusing. On p 124, k&r use the
syntax "enum { NAME....etc}; without anything between the "enum" and
the "{".From the appendix I think this is legal...in fact, if it is in
K&R I assume it is legal. And like you, I also asked how foo.c could
thus know about it in main.c, hence I added a "name" (perhaps syntax
was a poor choice for a name)
.
>
A char array is defined in main.c
" char token[10]; "
>
char *p = token " initialization makes pointer from integer without a
cast"

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.

Point taken...
>

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...........
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.
If you don't like it, neither will I!!
>
My preferred approach is MOVE the lines from
* * * * #include <stdio.h>
through
* * * * int gettoken(void);

OK...so this includes " enum {NAME, ......};
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.
Barry...I actually think to a large extent I have been doing this all
along. But I could be wrong. I have all my definitions in foo.c, all
my declarations in foo.h ( include foo.h in foo.c) and include foo.h
in main.c The part that is new to me is the use of extern, but I
think I have am getting the hang of it.
One question though. If one includes enum {NAME...} in foo.h, is this
not a definition. I was under the impression, that definitions should
not be included in .h files. ( I guess the same question would apply
to #define MAXTOKEN 100.} Or are these simple pre-processor macros?
Sorry if I have misunderstood your answer and you have already
answered this.
Thanks

Aug 4 '08 #4
mdh
On Aug 5, 10:23*am, Barry Schwarz <schwa...@dqel.comwrote:
On Tue, 5 Aug 2008 05:53:57 -0700 (PDT), mdh <m...@comcast.netwrote:
So is it fair to say one can use enums in 2 general ways.

I think this is true but I don't know if it is complete.
Two...with an identifier,....
......snip

....
>
I need to **declare** it either in the header or
before using it in that **other** function.

Just like a macro name.
Thank you for that. I think section 6 ( K&R) will give me a little
more insight into enumerators. For some reason, K&R go into very
little detail about this ( other than describing how to define it) ,
and the only example I saw where it is used with an identifier was in
one of the exercises (2.2) in which T&G use that technique to write a
solution. So your explanation has been very helpful.
Aug 6 '08 #5
mdh
On Aug 5, 12:18*pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
Barry Schwarz wrote, On 05/08/08 18:23:

Two...with an identifier, in which case one can declare a variable of
type "enum myenum" and assign to that variable one of the list
constants ?
In either case, if I wish to used that enum in another file ( ?
translation unit) I need to **declare** it either in the header or
before using it in that **other** function.
Just like a macro name.

Indeed. Also like a typedef or struct definition.
--
Those are next on the list! Thank you.
Aug 6 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.