473,385 Members | 1,461 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,385 software developers and data experts.

More structs

mdh
Quick question.
In the code below, omitting the word "word", as in char *word,
produces an error of syntax.

In similar "non-struct" declarations, the expression "char *" should
suffice. ( I think). Is there a reason that one needs to add this? Is
this unique to structs.
Thanks as usual.


struct key {
char *word;
int count;
};

int main (int argc, const char * argv[]) {

struct key keytab[] = {
"auto", 0
};

}
Aug 23 '08 #1
17 1215
mdh wrote:
Quick question.
In the code below, omitting the word "word", as in char *word,
produces an error of syntax.

In similar "non-struct" declarations, the expression "char *" should
suffice. ( I think). Is there a reason that one needs to add this? Is
this unique to structs.
Thanks as usual.

struct key {
char *word;
int count;
};

int main (int argc, const char * argv[]) {

struct key keytab[] = {
"auto", 0
};

}
If "word" were omitted, how would you make use of the
pointer that it names? Let's omit a few more identifiers:

struct {
char*;
int;
};
int (int, const char*[]) {
struct [] = {
"auto", 0
};
}

What sense can be made of this (invalid) source code? It
declares (sort of) a type, a function, two parameters, and
an array variable, but all are unusable.

Your phrase "similar ``non-struct'' declarations" I find
baffling. What "similar" declarations do you refer to?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 23 '08 #2
mdh <md**@comcast.netwrote:
Quick question.
In the code below, omitting the word "word", as in char *word,
produces an error of syntax.
In similar "non-struct" declarations, the expression "char *" should
suffice. ( I think).
What do you mean with '"non-struct" declarations'? A line with
just

char *;

is always a syntax error, also outside a structure declaration.
Is there a reason that one needs to add this? Is
this unique to structs.
How would you access the 'word' member of the structure if it
wouldn't have a name? Write-only (and that only during initia-
lization) members of a structure hardly make much sense...
struct key {
char *word;
int count;
};
int main (int argc, const char * argv[]) {
struct key keytab[] = {
"auto", 0
};
}
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Aug 23 '08 #3
mdh
On Aug 23, 12:10*pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
mdh wrote:
Quick question.

* * *If "word" were omitted, how would you make use of the
pointer that it names? *Let's omit a few more identifiers:

* * * * struct {
* * * * * * char*;
* * * * * * int;
* * * * };
* * * * int (int, const char*[]) {
* * * * * * struct [] = {
* * * * * * * * "auto", 0
* * * * * * };
* * * * }

What sense can be made of this (invalid) source code? *It
declares (sort of) a type, a function, two parameters, and
an array variable, but all are unusable.

* * *Your phrase "similar ``non-struct'' declarations" I find
baffling. *What "similar" declarations do you refer to?


At the risk of further ridicule, I **thought** this a valid
comparison.

void foo ( int a, char c){

int b;
char c;

b = a;

etc

}
Now the declaration, if I am not mistaken, could be

void foo ( int, char);
Clearly, this is different, but now sure why.
>

Aug 23 '08 #4
mdh
On Aug 23, 12:13*pm, j...@toerring.de (Jens Thoms Toerring) wrote:
mdh <m...@comcast.netwrote:
Quick question.
In the code below, omitting the word "word", as in char *word,
produces an error of syntax.
In similar "non-struct" *declarations, the expression "char *" should
suffice. ( I think).

What do you mean with '"non-struct" *declarations'? A line with
just
>
Well, as I tried to hopefully explain that in the answer to Eric.
I think what I might be missing is this issue of declaration/
definition, which rjh addressed in another thread...which I will go
back to and look at again.

So, this what I thought.

If I declare a struct thus.

struct key {
char * word;
int count;
};
Then omitting the word "word" could be added later in the
defintion...but clearly this is not the case.

char *;

is always a syntax error, also outside a structure declaration.
I **thought** that
void foo ( char *); as a declaration

is suitable for a definition later as
void foo ( char *c){};

Thanks for your input.
Aug 23 '08 #5
mdh wrote:
On Aug 23, 12:10 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>mdh wrote:
>>Quick question.
If "word" were omitted, how would you make use of the
pointer that it names? Let's omit a few more identifiers:

struct {
char*;
int;
};
int (int, const char*[]) {
struct [] = {
"auto", 0
};
}

What sense can be made of this (invalid) source code? It
declares (sort of) a type, a function, two parameters, and
an array variable, but all are unusable.

Your phrase "similar ``non-struct'' declarations" I find
baffling. What "similar" declarations do you refer to?

At the risk of further ridicule, I **thought** this a valid
comparison.
There was no intent on my part to ridicule you. When I
wish to ridicule someone, I make sure to leave no doubt (and
I usually regret it afterward).
void foo ( int a, char c){

int b;
char c;

b = a;

etc

}
Okay, all identifiers in all declarations present and
correct. No problem.
Now the declaration, if I am not mistaken, could be

void foo ( int, char);

Clearly, this is different, but now sure why.
The thing being declared here is the function foo(),
and its identifier is present and accounted for. The function
parameters are not declared, and would not be declared even
if you gave names for them. The names, if present, are really
just documentation; all that matters is their number and types.

For example, you might also write the declaration as

void foo(int c, char a);

Observe that the names in this declaration are c and a, while
in the actual function definition they are a and c. You could
also have written

void foo(int fibber, char molly);
or
void foo(int Char, char Int);
or
void foo(int xyzzy33, char xyzzy91);

The parameter names in these declarations declare nothing at
all; only foo itself is declared.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 23 '08 #6
mdh
On Aug 23, 2:22*pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
mdh wrote:
On Aug 23, 12:10 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
mdh wrote:
Quick question.
* * *If "word" were omitted, how would you make use of the
pointer that it names? *Let's omit a few more identifiers:
* * * * struct {
* * * * * * char*;
* * * * * * int;
* * * * };
* * * * int (int, const char*[]) {
* * * * * * struct [] = {
* * * * * * * * "auto", 0
* * * * * * };
* * * * }
>
At the risk of further ridicule, I **thought** this a valid
comparison.

* * *There was no intent on my part to ridicule you. *When I
wish to ridicule someone, I make sure to leave no doubt (and
I usually regret it afterward).

**Little OT**
Eric...a quick apology at a failed attempt at humor. I should have
added a huge smiley to the sentence.
Eric...you have, when ever you have answered me, been gracious and
informative, and I have always appreciated it ever time and learnt
from it. I am sorry if I implied anything you clearly did not mean.
** End of Little OT**

>
void foo ( int a, char c){
int b;
char c;
b = a;
etc
}

* * *Okay, all identifiers in all declarations present and
correct. *No problem.
Now the declaration, if I am not mistaken, could be
void foo ( int, char);
Clearly, this is different, but now sure why.

* * *The thing being declared here is the function foo(),
and its identifier is present and accounted for. *The function
parameters are not declared....
So these ( ie function parameters) are only declared at the
"definition level".

>
The parameter names in these declarations declare nothing at
all; only foo itself is declared.
I think I see what I am missing. Now , let me pursue the struct
declaration a little further.

In the example I used,

struct key {
char *word;
int count;
};

one could say that ??

"word", "count" and "key" are identifiers? which will be used later.
Aug 24 '08 #7
mdh
On Aug 23, 5:58*pm, Chris Torek <nos...@torek.netwrote:
[on omitting identifiers in some situations]
mdh wrote:
>Now the declaration, if I am not mistaken, could be
void foo (int, char);

In article <CrmdnY8vh_I54i3VnZ2dnUVZ_qzin...@comcast.com>
Eric Sosman *<esos...@ieee-dot-org.invalidwrote:
* * The thing being declared here is the function foo(),
and its identifier is present and accounted for. *The function
parameters are not declared, and would not be declared even
if you gave names for them. *The names, if present, are really
just documentation; all that matters is their number and types.

Right (of course :-) ).

Moving the question back a bit, to "why do function prototypes
have optional identifiers in the prototype-scope portion of the
declaration between the parentheses", the answer is: "historical
reasons". *

K&R-1 C did not have function prototypes. *To declare function

foo, you wrote:

* * int foo(); /* K&R-1 C also lacked "void" */

snip...

C++ came along, and changed all this. *
snip.

C++ was done by a guy with less good taste than Dennis
snip

The C Standards group came along and stole prototypes wholesale
from C++, optional identifiers and all. *They did make one more
change, to make the syntax even klunkier:


Thank you Chris for that very nice perspective. Much appreciated.
Aug 24 '08 #8
mdh
On Aug 23, 12:13*pm, j...@toerring.de (Jens Thoms Toerring) wrote:
>
What do you mean with '"non-struct" *declarations'? A line with
just

char *;

is always a syntax error, also outside a structure declaration.

I've had a chance to reflect on all that has been written, and reread
rjh's discussion of definition/declarations. I *think* I have figured
out where I erred, but would like to summarize it here, for some
feedback.

I have a file, k_rfunctions.c where I have collected exercises for re-
use. All the declarations go into k_rfunctions.h

So, when declaring struct key, I placed this in k_rfunctions.h,
thus...

struct key {
char *word;
int count;
};

Then in main, I **thought** I was defining the struct thus...(which I
initially incorrectly stated in my question)

struct key {
char *word;
int count;
}keytab[]= {
"auto", 0,
"break", 0
/*...*/
};
That's why I initially removed the word "word" and "count" from the
declaration, as I thought I would simply add it in later when defining
the struct.

But, I **think** the correct interpretation of what I was doing was to
define a new type, "struct key", which can only be defined once within
the same scope. ( per rjh). I think what I missed was the fact this
new type **is** char *word, int count as opposed to something simple,
up to now like int a, char b etc.
Anyhow, hope this makes a little more sense.
Aug 24 '08 #9
mdh <md**@comcast.netwrites:
Quick question.
In the code below, omitting the word "word", as in char *word,
produces an error of syntax.

In similar "non-struct" declarations, the expression "char *" should
suffice. ( I think). Is there a reason that one needs to add this? Is
this unique to structs.
Thanks as usual.

struct key {
char *word;
int count;
};

int main (int argc, const char * argv[]) {

struct key keytab[] = {
"auto", 0
};

}
I think your question has been answered fairly thoroughly, but
I'm going to take a shot at it anyway.

The only "non-struct declarations" I can think of in which the
identifier is optional are function declarations. There's some
superficial similarity between a struct declaration and a function
declaration, but they're really two different things.

The parameter names in a function declaration are optional because the
names aren't used in a function call. But the names are needed in the
function definition, and so they're mandatory there.

For example:

void foo(int); /* name not needed by caller */

...

void foo(int x) { ... } /* Here the name needed so the code can
refer to the parameter */

For a struct declaration, on the other hand, there's not much you can
do without referring to the member names; that's why the member names
are mandatory.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 24 '08 #10
mdh wrote:
Quick question.
In the code below, omitting the word "word", as in char *word,
produces an error of syntax.

In similar "non-struct" declarations, the expression "char *" should
suffice. ( I think). Is there a reason that one needs to add this? Is
this unique to structs.
Thanks as usual.


struct key {
char *word;
int count;
};

int main (int argc, const char * argv[]) {

struct key keytab[] = {
"auto", 0
};

}
I think that you may have been thinking of a foward declaration
that does not fully define the type.

/* BEGIN new.c */

struct key; /* This is an incomplete declaration */

int main(void)
{
struct key {
char* word;
int count;
} keytab[] = {
"auto", 0
};

return keytab[0].count;
}

/* END new.c */
--
pete
Aug 24 '08 #11
mdh wrote:
[...]
I think I see what I am missing. Now , let me pursue the struct
declaration a little further.

In the example I used,

struct key {
char *word;
int count;
};

one could say that ??

"word", "count" and "key" are identifiers? which will be used later.
All three are identifiers; whether they are or are not
used later is unknown. ;-)

They are identifiers for different kinds of things. "key"
is declared as a struct tag, part of the type name `struct key'.
"word" and "count" are declared as elements of a `struct key',
names to be used along with an instance of a `struct key' or a
pointer to a `struct key'.

In ancient C, all these identifiers inhabited the same
"name space," meaning that a single identifier could refer to
only one thing at a time. With the declaration shown above,
the identifiers "key", "word", and "count" could refer only
to the struct type and its elements. You could not, for
example, have a variable named "word" or declare another struct
type with its own "count" element.[*] Some vestiges of this
restriction can still be seen: For example, ponder the names
of the elements of a `struct tm'.

Modern C has a notion of name spaces that allows the same
identifier to be used for different purposes depending on
context. Taken by themselves, "word" and "count" do not refer
to struct elements; they only have that meaning when used in
connection with a `struct key' reference. This means that you
can declare other struct and union types with their own "word"
and "count" elements that have nothing to do with the contents
of a `struct key'. You can even declare free-standing variables
and function parameters named "word" and "count" without creating
a conflict. This is a great convenience: For example, you can
declare several different struct types each with its own "next"
element for use in linked lists, and need not fret about making
them "next_key", "next_year", "next_door_neighbor", and so on
as in the Old Days.
[*] Re-using an identifier in a separate scope is a different
matter.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 24 '08 #12
mdh
On Aug 24, 5:53*am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>'.

* * *In ancient C, all these identifiers inhabited the same
"name space," ........... *Some vestiges of this
restriction can still be seen
snip
* * *Modern C has a notion of name spaces......... *This means that you
can declare other struct and union types with their own "word"
and "count" elements that have nothing to do with the contents
of a `struct key'.

Thank you Eric. I think I finally get it.
Aug 24 '08 #13
mdh
On Aug 24, 12:41*am, Keith Thompson <ks...@mib.orgwrote:
mdh <m...@comcast.netwrites:
Quick question.
I think your question has been answered fairly thoroughly, but
I'm going to take a shot at it anyway.

The only "non-struct declarations" I can think of in which the
identifier is optional are function declarations.
As I thought about it more, this is the **only** situation I could
think of as well.

*There's some
superficial similarity between a struct declaration and a function
declaration, but they're really two different things.

The parameter names in a function declaration are optional because the
names aren't used in a function call. *But the names are needed in the
function definition, and so they're mandatory there.

For a struct declaration, on the other hand, there's not much you can
do without referring to the member names; that's why the member names
are mandatory.

I think where there is a grey zone in semantics, is **even** in a
struct declaration, one is ?? defining a new type?? which is the way I
will remember that all the members need to be *declared* :-) in full.
Either way, these things are so intuitive to the more experienced in
the clc, and hopefully I will someday get there and just not worry
about the minutia! :-)

Your point though does reinforces what I need to know.
Thanks for your input.
Aug 24 '08 #14
mdh <md**@comcast.netwrites:
[...]
I think where there is a grey zone in semantics, is **even** in a
struct declaration, one is ?? defining a new type?? which is the way I
will remember that all the members need to be *declared* :-) in full.
Either way, these things are so intuitive to the more experienced in
the clc, and hopefully I will someday get there and just not worry
about the minutia! :-)
Yes, a struct declaration defines a new type.

But then, a function declaration *also* defines a new type, namely a
function type. (Yes, it really is a type; you can declare a pointer
to it.) And they're superficially similar in that both incorporate a
list of other types:

struct s { int a; double b; };
void f(int a, double b);

And *if* the language allowed you to declare a struct type without
specifying the member names:

struct s { int; double;}; /* not valid C */

then there are still some things you could do with it:

struct s obj1 = { 42, 1.5 };
struct s obj2 = obj1;

There's no *fundamental* reason C had to be defined the way it was.
Some languages let you have a composite type consisting of members
of other types where the members aren't named (e.g., Python's
"tuple"). Some languages require names for function (or procedure,
or subroutine) parameters, and allow you to use those names in
calls; for example, the Ada equivalent of the "f" function above
might be called either as F(42, 1.5) or as F(A =42, B =1.5),
or even F(B =1.5, A =42).

But since C function calls don't require referring to the parameter
names (and in fact don't allow it), and since most uses of structs do
require the use of the member names, it just made sense to require
names for struct members but not for function parameters.

Except that function parameters must be given names in a function
*definition*, because the code that implements the function can't
refer to the parameters unless their names are visible.

<OT>C++ allows parameter names to be omitted if the parameter
isn't used. This turns out to be useful in C++, but not in C.</OT>

I would have been happier if the language had required consistency
between function declarations and definitions, but we've managed
to muddle along even without a language that follows my every
personal whim.
Your point though does reinforces what I need to know.
Thanks for your input.
I hope I haven't just caused more confusion.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 24 '08 #15
mdh
On Aug 24, 11:22*am, Keith Thompson <ks...@mib.orgwrote:
>
There's no *fundamental* reason C had to be defined the way it was.
snip

I hope I haven't just caused more confusion.

No...it helps having it put into perspective. Thank you.

Aug 24 '08 #16
Keith Thompson <ks***@mib.orgwrites:
mdh <md**@comcast.netwrites:
[...]
I think where there is a grey zone in semantics, is **even** in a
struct declaration, one is ?? defining a new type?? which is the way I
will remember that all the members need to be *declared* :-) in full.
Either way, these things are so intuitive to the more experienced in
the clc, and hopefully I will someday get there and just not worry
about the minutia! :-)

Yes, a struct declaration defines a new type.

But then, a function declaration *also* defines a new type, namely a
function type. (Yes, it really is a type; you can declare a pointer
to it.)
I'm not sure what you're meaning to say here. A struct declaration
does define a new type, but a function declaration (or definition)
does not. For example:

struct { int x; } s_one;
struct { int x; } s_two;

int f_one( int, double );
int f_two( int, double );

The type of f_one and the type of f_two are the same type, namely
'int (*)(int,double)'. This type exists independently of any
function declarations.

The type of s_one and the type of s_two are not the same type,
because they are both new types (distinct from every other type
in the program).

These type (non-)equivalences can easily be seen by pointer
comparison:

&f_one == &f_two

&s_one == &s_two

The first comparison works fine, because the function types are
the same; the second comparison gives a diagnostic, because the
two types are different from each other.

Sorry to be such a nit on this; I've had this point about
structs and types drilled into me by Chris Torek. :)
Oct 9 '08 #17
Tim Rentsch <tx*@alumnus.caltech.eduwrites:
Keith Thompson <ks***@mib.orgwrites:
>mdh <md**@comcast.netwrites:
[...]
I think where there is a grey zone in semantics, is **even** in a
struct declaration, one is ?? defining a new type?? which is the way I
will remember that all the members need to be *declared* :-) in full.
Either way, these things are so intuitive to the more experienced in
the clc, and hopefully I will someday get there and just not worry
about the minutia! :-)

Yes, a struct declaration defines a new type.

But then, a function declaration *also* defines a new type, namely a
function type. (Yes, it really is a type; you can declare a pointer
to it.)

I'm not sure what you're meaning to say here. A struct declaration
does define a new type, but a function declaration (or definition)
does not.
I meant to say exactly what I said. The fact that I was wrong is
beside the point. 8-)}

[...]
Sorry to be such a nit on this; I've had this point about
structs and types drilled into me by Chris Torek. :)
No need to apologize. Thanks for the correction.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 9 '08 #18

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

Similar topics

4
by: news.microsoft.com | last post by:
Hi, I am using structs and am also using property accessors to access those private member fields... TO me this is a good way of handling them, but I find alot of people using direct access to...
3
by: Sourin | last post by:
Hi all, I am trying to write code for my experiments. My work involves huge datasets, and as such my code needs to be memory efficient. I did some hand calculations regarding the amount of...
6
by: James Pascoe | last post by:
Dear All, Apologies if this is OT. I have a C program which processes an arbitrary number of structs that are stored in a hash table. (The nature of the processing and the layout of the...
3
by: p988 | last post by:
Learning C# is much tougher than I expected...Please help me by answering the following questions! Thank you in advance! 1. Are all Enumerations type Value type? 2. The line, RegistryKey...
10
by: Bonj | last post by:
I almost understand TSTs, to the point where I just need to know the answer to this: When making a TST (in C++) that will have as its leaf nodes words that make up SQL language and an categorising...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
1
by: ajtaylor | last post by:
I have a large amount of C++ unmanaged code that I am attempting to use in a managed project. Amongst this code is a large number of classes and structs that I would like to be able to use in...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
3
by: Pietro Cerutti | last post by:
Hi Group, suppose test1.c, test2.c and test.h /*** BEGIN TEST.H ***/ #ifndef _TEST_H #define _TEST_H typedef struct {
8
by: kiser89 | last post by:
I'm having a problem with my array of structs and segmentation faults. I have this struct that represents one line of a source file: struct threeTokens { int lineNumber; char* cmd; char* param;...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.