tree errors

B

Bill Cunningham

Keith Thompson said:
By "this below" do you mean the following paragraph or the code?


Right. The "." operator requires an expression of struct type as its
left operand. The "->" operator requires an expression of
pointer-to-struct type as its left operand. In either case, providing
anything else violates a constraint, which means that the compiler is
required to produce a diagnostic. (For "struct", read "struct or
union".)

(Note that if you're using gcc, some constraint violations result in
warnings, not error messages. Both are diagnostics, and neither
should be ignored.)

On to the code you posted. Did you compile it? If so, why aren't you
showing us the diagnostic messages?

The code I posted was from a tutorial. I did not write or compile it.
The tutorial must be in error then.

http://www.learn-programming.za.net/programming_c_learn09.html

This is where the code is from. I assumed it was good.

Bill
 
K

Keith Thompson

You didn't answer that question.

[snip]
The code I posted was from a tutorial. I did not write or compile it.
The tutorial must be in error then.

http://www.learn-programming.za.net/programming_c_learn09.html

This is where the code is from. I assumed it was good.

Yes, the tutorial is incorrect; please stop using it.

I suspect that the author, when writing

PERSON p;
PERSON *pptr;
PERSON pptr = &p;

intended to write:

PERSON p;
PERSON *pptr;
pptr = &p;

which would have been valid. But anyone who would post code in a web
tutorial without bothering to compile it is likely to be
insufficiently competent to be trusted. Run, do not walk, away from
this tutorial.

There are several good C tutorials out there. Someone will post URLs
Real Soon Now.

And if you're going to post code that you got from another source, it
would be helpful if you'd tell us where you got it; a URL should be
sufficient.
 
B

Bill Cunningham

[snip]
And if you're going to post code that you got from another source, it
would be helpful if you'd tell us where you got it; a URL should be
sufficient.

I did post a URL.
 
O

osmium

"Bill Cunningham"

I can't really follow what the subject is, for sure, what with your musings
about what it should have been and what you now think, what jargon words
mean and on and on and on. I think there is some possibility that the
following code is the current subject and it has already been commented on
by others. If, by any chance, the above is correct, let me point out at
least one problem.
#include <stdio.h>

struct Cat {
char *name;
char *color;
int age;
};

int main(void)
{
struct Cat c;
c.name = "snowball";
c.color = "white";
c.age = 9;

printf("%s %s %i\n", c.name, c.color, c.age);
}

***THAT*** right brace marks the end of main. What is the following stuff,
which just dribbles off the end? If you want main to end someplace else,
move the brace where it should be. Count the braces Bill, count the braces.
 
O

osmium

Bill Cunningham said:
[snip]
And if you're going to post code that you got from another source, it
would be helpful if you'd tell us where you got it; a URL should be
sufficient.

I did post a URL.

You only posted it AFTER you were badgered for it. Post it at the same time
you post your initial question.
 
S

Squeamizh

turn all your compiler warnings on. Don't ignore warnings.
Think of it as like the oil warning light in a car. It means
something
is wrong (usually).





no.
    printf ("%p", p);
does not "print the address pointed to" it prints the value of
p in pointer (or address) format. You seem confused about pointers
so careful use of language is important.

If Bill Cunningham were sincerely a struggling learner (which he
clearly is not) then what you said above would have been exactly the
wrong thing to tell him. Someone trying to learn pointers could never
make sense out of the correction you posted, it is way too advanced of
a concept. How long has Bill supposedly learning pointers? And you
don't recognize the futility in posting that correction?
 
N

Nick Keighley

If Bill Cunningham were sincerely a struggling learner (which he
clearly is not)

opinions vary on this. Even mine.
then what you said above would have been exactly the
wrong thing to tell him.

I disagree. Bill posted something that was wrong. I corrected it.
I cannot see how doing this could ever be wrong.
 Someone trying to learn pointers could never
make sense out of the correction you posted,

I don't see why not. The english could be better as you kind of have
to
read it twice. Better might be:-

"no.
printf ("%p", p);
does not "print the address pointed to". It prints the value of p.
It prints it in pointer (that is, address) format."

happy now? Because if Bill doesn't know the difference between
a pointer value and a pointer variable he is going to be confused for
ever (I don't rule this out as a possibility...). In general someone
who doesn't know the difference between a variable and a value is
always going to have problems with computers (and probably life as
well!).
it is way too advanced of a concept.

rubbish. It's the "A is for Apple" of computer programming
 How long has Bill supposedly learning pointers?

Veloceraptor's were still posting when he started.

 And you
don't recognize the futility in posting that correction

nope. Sure don't. It might teach Bill something. It might teach
someone else something. It might provoke an interesting discussion.
The horse might learn to sing.


--
[long sig but I like the quote]
[if you follow this you can understand pointers!]


You are sad,' the Knight said in an anxious tone:
`let me sing you a song to comfort you.'

`Is it very long?' Alice asked, for she had heard a
good deal of poetry that day.

`It's long,' said the Knight, `but it's very, very beautiful.
Everybody that hears me sing it -- either it brings the tears
into their eyes, or else --'

`Or else what?' said Alice, for the Knight had made a sudden pause.

`Or else it doesn't, you know. The name of the song is called
"Haddocks' Eyes".'

`Oh, that's the name of the song, is it?' Alice said, trying
to feel interested.

`No, you don't understand,' the Knight said, looking a little vexed. `
That's what the name is called. The name really is "The Aged Aged
Man".'

`Then I ought to have said "That's what the song is called"?'
Alice corrected herself.

`No, you oughtn't: that's quite another thing! The song is called
"Ways and Means": but that's only what it's called, you know!'

`Well, what is the song, then?' said Alice, who was by this
time completely bewildered.

`I was coming to that,' the Knight said. `The song really
is "A-sitting On a Gate": and the tune's my own invention.'

-- Alice Through The Looking Glass
 
O

osmium

Richard Heathfield said:
Ha! You make me sound like a Pope or something.


I think so, yes. Steve Summit and Tom Torfs both know their stuff. I
do not, however, make the claim that those tutorials are perfect.
What is?

Nicole Kidman.
 
D

Default User

Squeamizh said:
How long has Bill supposedly learning pointers? And you
don't recognize the futility in posting that correction?

Well, he's supposedly been learning C for a long time now. I though it
was like ten years, but the earliest posts I can find with the less
than reliable Google Groups search are from 2002.

At any rate, if he's been studying for seven years and made no more
progress than this, I personally don't think it's the gig for him.
Assuming it's not all a big long troll. It seems to me, from reading
his old C++ posts, that his overall knowledge seemed better back then.




Brian
 
B

Bill Cunningham

Richard Heathfield said:
Ha! You make me sound like a Pope or something.


I think so, yes. Steve Summit and Tom Torfs both know their stuff. I
do not, however, make the claim that those tutorials are perfect.
What is?

<snip>

I seem to be getting the same errors trying this.
#include <stdio.h>

struct cat {
char *name;
char *color;
};

struct cat P;
struct cat *pp;
pp = &P;

main()
{
pp->name = "Snowball";
pp->color = "white";
printf("%s %s\n", pp->name, pp->color);
return 0;
}

p.c:10: conflicting types for `pp'
p.c:9: previous declaration of `pp'
p.c:10: warning: data definition has no type or storage class
p.c: In function `main':
p.c:14: invalid type argument of `->'
p.c:15: invalid type argument of `->'
p.c:16: invalid type argument of `->'
p.c:16: invalid type argument of `->'

I dunno.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I seem to be getting the same errors trying this.

Thank you for posting the full source and error messages.
#include <stdio.h>

struct cat {
char *name;
char *color;
};

struct cat P;
struct cat *pp;
pp = &P;

This is an assignment statement. Statements are legal only inside
function bodies.

This should be "int main(void)", though that's not the cause of your
problem.
{
pp->name = "Snowball";
pp->color = "white";
printf("%s %s\n", pp->name, pp->color);
return 0;
}

p.c:10: conflicting types for `pp'
p.c:9: previous declaration of `pp'
p.c:10: warning: data definition has no type or storage class
p.c: In function `main':
p.c:14: invalid type argument of `->'
p.c:15: invalid type argument of `->'
p.c:16: invalid type argument of `->'
p.c:16: invalid type argument of `->'

For small toy programs like this, it's reasonable to do everything
inside the main function. Take all the lines of code between (but not
including) the "#include stdio.h>" and the opening "{" of main, and
move it just after the opening "{". When I tried that, the program
compiled and ran without error.
 
D

David Thompson

In <[email protected]>, Bill Cunningham
wrote:

Start considering it. I can't believe you're not paying attention to
"warnings" (or diagnostic messages, which is the Standard's term for
both "warnings" and "errors").
Strongly agree.
Ideally, you want a clean compile from:

gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-ffloat-store -O2 -fno-builtin -g -pg -c -o foo.o foo.c

But not entirely. I would add -Wold-style-definition -Wno-multichar
-Wunreachable-code -Wendif-labels, but I would either exclude
-Wno-parentheses or at least ignore/allow some of its warnings,

and I would drop:
-Winline (useful for optimization doesn't matter for correctness)
-Wcast-align (you should be skeptical of ALL casts, but if you
actually have a sufficiently-aligned pointer a 'down'cast is OK)
-fno-builtin (good for debugging doesn't matter for correctness)

and I personally use -g but it isn't needed for clean source, and I
usually WOULDN'T use -pg and certainly wouldn't require it.

And I wouldn't unnecessarily specify the default -o for -c,
and I would use the clearer new name -Wextra for -W.
And for people who like Bill don't see warnings I would add -Werror.

Whew.
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top