struct problems

G

Guest

As a matter of style, I wouldn't declare multiple objects in a single
line line that.  I'd write:
    struct cat kitty1 = { "striper", "striped" };
    struct cat kitty2 = { "spook",   "black"   };

His style is far nicer IMO. Why specify the type twice?
Localisation. Compact. Clean.

int* pi, pj;

the person who wrote that might think pj was an int*.
If you want compact use a typedef.

Cat kitty1;
Cat kitty2;

--
Nick Keighley

Every sentence I utter must be understood not as an affirmation,
but as a question.
Niels Bohr
 
B

Ben Bacarisse

[...]
    I didn't know that the assignments couldn't be done outside of a
function.
An assignment (when followed by a semicolon) is a statement, and
statements can occur only inside functions.
So you are saying a line like
int bill = 1;
is NOT legal outside a function?

no. but then it isn't a statement

that of course should have read
"yes. But then it isn't a statement"

Nor is it an assignment (which is, of course, why it is not a
statement).
 
R

Richard

Ben Bacarisse said:
[...]
    I didn't know that the assignments couldn't be done outside of a
function.

An assignment (when followed by a semicolon) is a statement, and
statements can occur only inside functions.

So you are saying a line like

int bill = 1;

is NOT legal outside a function?

no. but then it isn't a statement

I didnt say it was.

And the "no", which bit is ita "no" to?

uh huh...
Nor is it an assignment (which is, of course, why it is not a
statement).

Mere mortals do indeed, in the real world, say it IS an
assignment. Which is why, of course, I asked the question. I would refer
you to who is involved in the thread and the "non assignment" Bill was
doing.

You see

,----
| struct MSG {
| int m;
| char * p;
| };
|
| struct MSG msg = {1,"Hello"};
`----

compiles for me. And this is VERY similar to what Bill was doing only to
be told it's illegal.
 
R

Richard

Keith Thompson said:
[...]
struct cat kitty1,kitty2;
kitty1={"striper","striped"};
kitty2={"spook","black"};

And this is the actual problem.  You're presumably trying to initialize
these structure variables.  You want
struct cat
  kitty1 = { "striper", "striped" },
  kitty2 = { "spook",   "black"   };
As a matter of style, I wouldn't declare multiple objects in a single
line line that.  I'd write:
    struct cat kitty1 = { "striper", "striped" };
    struct cat kitty2 = { "spook",   "black"   };

His style is far nicer IMO. Why specify the type twice?
Localisation. Compact. Clean.

int* pi, pj;

the person who wrote that might think pj was an int*.

And that person should not use ++ either no doubt.
If you want compact use a typedef.

typedefs are confusing as hell in my opinion and I tend not to use them.
Cat kitty1;
Cat kitty2;

Disagree. But thats the thing with style. You offered another style for
another case. the "*" never came into it.

BTW, FYI, your .sig separator is incorrect. It should be "-- ". Hence my
autosnipper has not removed it on the reply.
 
G

Guest

Ben Bacarisse said:
On 20 Jan, 10:21, (e-mail address removed) wrote:
[...]
    I didn't know that the assignments couldn't be done outside of a
function.
An assignment (when followed by a semicolon) is a statement, and
statements can occur only inside functions.
So you are saying a line like
int bill = 1;
is NOT legal outside a function?
no. but then it isn't a statement

I didnt say it was.

And the "no", which bit is ita "no" to?

I'm saying (badly) that the line *is* legal outside
a function, because it isn't a statement.

So why do you think Keith Thompson's statement implied the line
was not legal outside a function if you didn't think the line was
a statement?
uh huh...

yeh, I know

Mere mortals do indeed, in the real world, say it IS an
assignment.

they do? They may think it looks like an assignment but it isn't.
Which is why, of course, I asked the question.

er, why did you ask the question? You knew the answer.

I would refer
you to who is involved in the thread and the "non assignment" Bill was
doing.

You see

,----
| struct MSG {
|       int m;
|       char * p;
| };
|
| struct MSG msg = {1,"Hello"};
`----

compiles for me. And this is VERY similar to what Bill was doing only to
be told it's illegal.

its similar but it plainly isn't the same.

I thought you believed Bill was secretly a genius computer
programmer who was very cleverly yanking our chain?

Or are we talking the false bill persona who pretends to
to be stupid and forgettful? he might confuse initialisation
and assignment.
 
R

Richard

Ben Bacarisse said:
(e-mail address removed) writes:
On 20 Jan, 10:21, (e-mail address removed) wrote:
[...]
    I didn't know that the assignments couldn't be done outside of a
function.
An assignment (when followed by a semicolon) is a statement, and
statements can occur only inside functions.
So you are saying a line like
int bill = 1;
is NOT legal outside a function?
no. but then it isn't a statement

I didnt say it was.

And the "no", which bit is ita "no" to?

I'm saying (badly) that the line *is* legal outside
a function, because it isn't a statement.

So why do you think Keith Thompson's statement implied the line
was not legal outside a function if you didn't think the line was
a statement?
uh huh...

yeh, I know

Mere mortals do indeed, in the real world, say it IS an
assignment.

they do? They may think it looks like an assignment but it isn't.

Yes, they do.

"Assign 1 the global here". The assigment symbol "=" is the giveaway
that not everyone speaks "standard".
er, why did you ask the question? You knew the answer.

Try to think. To suggest that one needs to get ones vocab RIGHT when
dealing with nOObs.
its similar but it plainly isn't the same.

I thought you believed Bill was secretly a genius computer
programmer who was very cleverly yanking our chain?

Or are we talking the false bill persona who pretends to
to be stupid and forgettful? he might confuse initialisation
and assignment.

Or that you are so wrapped up in "Standard" you can not see that in the
real world this IS an assignment.

And your sig is still wrong. dash-dash-space or "-- " is the separator.
 
B

Ben Bacarisse

"Assign 1 the global here". The assigment symbol "=" is the giveaway
that not everyone speaks "standard".

It has nothing to do with "speaking standard". You can't program in C
without knowing the difference. There are huge differences between
what can be done in an assignment and what is permitted in an
initialisation. It is not even limited to C. Lots of languages have
this distinction though some, helpfully, have more distinctive syntax.
While these exatra distinctions help, the issue is hardly obscure in C
-- you very soon learn the syntactic difference between a statement
and a declaration.

Or that you are so wrapped up in "Standard" you can not see that in the
real world this IS an assignment.

The only worlds in which it is an assignment are imaginary ones. Yes,
there are lots of people who don't know the difference, and are
probably constantly baffled by error messages (that talk about
assignments in some cases and initialisations in others) and who can't
work out why some things seem OK in one place and not in another, but
they are simply ill-informed, not living in a world that is any more
real than the one I inhabit.
 
R

Richard

Ben Bacarisse said:
It has nothing to do with "speaking standard". You can't program in C
without knowing the difference. There are huge differences between

I have done for years. Well, understanding what people mean and not
being anal about it.
what can be done in an assignment and what is permitted in an
initialisation. It is not even limited to C. Lots of languages have
this distinction though some, helpfully, have more distinctive syntax.
While these exatra distinctions help, the issue is hardly obscure in C
-- you very soon learn the syntactic difference between a statement
and a declaration.

Many professional programmers never, ever learn the CORRECT terminology
since its, well, not required to be a good programmer. One knows what
one means. I dont expect many people to agree with me.
The only worlds in which it is an assignment are imaginary ones. Yes,

No. I am assigning one to the variable. Sorry and all that. You can
call it initialise if you want.
there are lots of people who don't know the difference, and are
probably constantly baffled by error messages (that talk about

No. More baffled by people who refuse to "understand" the obvious and
hide behind vocabulary to remain "confused".
assignments in some cases and initialisations in others) and who can't
work out why some things seem OK in one place and not in another, but
they are simply ill-informed, not living in a world that is any more
real than the one I inhabit.

You seem to miss the point.

int b = 1;

It IS common vocabulary to say "Assign 1 to b".

Hence my reply about choosing language carefully when talking to people
like Bill.

It made it sound like you could not initialise a variable outside a
function. The fact is you can. The FACT is that (in my experience) MOST
people would still say we "assign 1 to b" in this case.

Clearly one can not assign a recently modified variable to b later on
outside the scope of a function since the execution path does not allow
that you are ONLY in the scope of functions.

We won't agree. Call me unprofessional, dense, etc etc if you want. But
I know what someone means when they say "assign 1 to be" in the
global/module scope. I dont feign confusion.
 
B

Ben Bacarisse

Richard said:
We won't agree. Call me unprofessional, dense, etc etc if you want. But
I know what someone means when they say "assign 1 to be" in the
global/module scope. I dont feign confusion.

Yes, we won't agree. I won't call you unprofessional if you return
the favour by not suggesting I am being disingenuous.
 
R

Richard

Ben Bacarisse said:
Yes, we won't agree. I won't call you unprofessional if you return
the favour by not suggesting I am being disingenuous.

There we go again with words ....
 
R

Richard Tobin

Richard said:
Or that you are so wrapped up in "Standard" you can not see that in the
real world this IS an assignment.

Perhaps, but in that case the real world is not relevant to whether
it's legal C. You need to know C's distinction between assignments
and initialisations to know what is allowed and what isn't.

Of course, you could come up with some other terminology to explain
it more clearly than the standard does, but so far you haven't.

-- Richard
 
R

Richard

Perhaps, but in that case the real world is not relevant to whether
it's legal C. You need to know C's distinction between assignments
and initialisations to know what is allowed and what isn't.

Yes and one does know. Its called execution path.
Of course, you could come up with some other terminology to explain
it more clearly than the standard does, but so far you haven't.

I am happy with "assign 1 to b" when I see

int b = 1;
 
R

Richard Tobin

Of course, you could come up with some other terminology to explain
it more clearly than the standard does, but so far you haven't.
[/QUOTE]
I am happy with "assign 1 to b" when I see

int b = 1;

And what do you call the second line here:

int b;
b = 1;

and how do you distinguish it from the assignment in your case, to
explain that it isn't allowed?

[I have a vague recollection that early C did initialisations without
the = operator, like this:

int b 1;

Would you still call this an assignment?]

-- Richard
 
R

Richard

I am happy with "assign 1 to b" when I see

int b = 1;

And what do you call the second line here:

int b;
b = 1;[/QUOTE]

Assignment of course. I would have though that obvious. Whether the
assignment is valid depends on scope.
and how do you distinguish it from the assignment in your case, to
explain that it isn't allowed?

err, scope.
[I have a vague recollection that early C did initialisations without
the = operator, like this:

int b 1;

Would you still call this an assignment?]

I dont know. I dont know what it is.

Richard, I am merely talking "plain talking". Context is always at
issue.

My main issue was simply its very easy to confuse people. It is NOT
clear to me why anyone would say "int b=1;" is NOT assigning 1 to b. I
prefer to talk "English" than language dialect when explaining
things. Especially to Bill...
-- Richard

I will graciously withdraw from this thread now to let the usual
suspects huff and puff but no one will convince me that "int b=1;"
can NOT be described as assigning 1 to the global/whatever variable
"b". Maybe I'm not scientific enough in my approach. But its worked for
me for donkeys years and have never, ever been in vocabulary wars in the
real world on real projects as a result of it.

Scope is key. Context thinking.
 
J

John Bode

So you are saying a line like

int bill = 1;

is NOT legal outside a function?

No. He's saying that an assignment statement (which is an expression
statement) is not allowed outside of a function definition. What you
have there is an intialized declarator, which is not the same thing as
an assignment statement. Both may have an assignment operator in the
middle, but they are not the same thing according to the language
grammar.

Or are you saying a line like

bill = 1;

IS legal outside of a function definition?
 
R

Richard

John Bode said:
Keith Thompson said:
[...]
    I didn't know that the assignments couldn't be done outside of a
function.
An assignment (when followed by a semicolon) is a statement, and
statements can occur only inside functions.

So you are saying a line like

int bill = 1;

is NOT legal outside a function?

No. He's saying that an assignment statement (which is an expression
statement) is not allowed outside of a function definition. What you

I know. Hence I posed the question.
have there is an intialized declarator, which is not the same thing as
an assignment statement. Both may have an assignment operator in the
middle, but they are not the same thing according to the language
grammar.

Sure. I always have and always will continue to refer to "int b=1;" as
"assigning 1 to be".
Or are you saying a line like

bill = 1;

IS legal outside of a function definition?

No. As you well know. I knew this would happen.
 
S

santosh

Richard said:
(e-mail address removed) writes:

And your sig is still wrong. dash-dash-space or "-- " is the
separator.

Trying to take the mantle from CBF are we? ;-)
 
R

Richard

Assignment of course. I would have though that obvious. Whether the
assignment is valid depends on scope.

Huh? How does the scope differ between the two cases?[/QUOTE]

err, it doesn't.

You are talking an "assignment". I am talking about the verb "to
assign".

I thought I had made that obvious in the context of confusing new
programmers.
I expected you to say something like "outside of functions, assignment
is only allowed as part of a declaration".

The word "assign" and "assignment" are key to c.l.c thinkers accepting
that I am not a terrible programmer because I say that int b=1;" is
assigning 1 to b in global scope outside of a function without being
confused.....

Me? I can cope with both words. Maybe I'm dumb. I don't know.

But "assigning a value to a variable" is language neutral for me pretty
much and I still describe

"int b=1;" as an assignment albeit outside of a function.

Naughty me I'm sure.

but lets review, once again : I suggested that without full and
confusing explanations it is wrong to explain to a noob that you can NOT
assign a value to a variable outside of a function since, in my books,
you clearly can.
 
R

Richard Tobin

And what do you call the second line here:

int b;
b = 1;
[/QUOTE]
Assignment of course. I would have though that obvious. Whether the
assignment is valid depends on scope.

Huh? How does the scope differ between the two cases?

I expected you to say something like "outside of functions, assignment
is only allowed as part of a declaration".

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top