tree errors

B

Barry Schwarz

It's possible that his code actually did compile. gcc, by default,
quietly accepts C-like code with nested function definitions,
such as this:

int outer(void) {
int inner(void) {
}
}

All well and good but it does not explain:
The missing diagnostic for converting the implied int returned by
malloc to a pointer.
The missing diagnostic for an invalid first argument to printf.
The missing diagnostic for the end of file in the middle of main.
 
B

Barry Schwarz

You should know that the OP has mental incapabilities! He knows of
that fact and has declared it as he was very new to this group. That
means that his memory likes to forget what he has learned, he learns -
but he learns slowly, really slowly and needs to repeat steps more
often than you may think of. We have already accepted his handicaps at
all.

Please be pationt with him. He does his best - even as this is not
much.

If his claimed infirmities induce the inability to see diagnostics and
the inability to post the code he is actually compiling, the effort is
doomed and patience would be a disservice.
 
K

Keith Thompson

Barry Schwarz said:
[...]
It's possible that his code actually did compile. gcc, by default,
quietly accepts C-like code with nested function definitions,
such as this:

int outer(void) {
int inner(void) {
}
}

All well and good but it does not explain:
The missing diagnostic for converting the implied int returned by
malloc to a pointer.
The missing diagnostic for an invalid first argument to printf.
The missing diagnostic for the end of file in the middle of main.

Agreed. I just fed his code to gcc with default options, and got 3
warnings (one about the implicit declaration of malloc, two about
printf calls) and one error (about the bad first argument to printf).

One quibble: there is no end of file in the middle of main, though the
horrible coding style Bill has chosen makes this difficult to see.
The last line of his code is:

printf(*pt);} /* tried printf(pt); also */

Note the nearly hidden '}'.

Bill, you seem to have forgotten my advice to filter your code through
"indent" (I recommend "indent -kr") before posting it.
 
B

Bill Cunningham

If his claimed infirmities induce the inability to see diagnostics and
the inability to post the code he is actually compiling, the effort is
doomed and patience would be a disservice.

If your idea of diagnostics is compiler warnings maybe I'm wrong but if
it compiles I don't consider it a big problem. If you mean a debugger like
gdb, I'm not quite sure how to use it. I know the -g switch is needed with
gcc to compile with debugging symbols.

Bill
 
B

Bill Cunningham

Bill, you seem to have forgotten my advice to filter your code through
"indent" (I recommend "indent -kr") before posting it.

I must've missed. I use it all the time now. indent -kr -nut.

Bill
 
B

Bill Cunningham

Show us an example of your success with this since your previous post
claimed you didn't understand structs.

[snip]

Ok I think I have structs now. I will post it soon. My HD had to be
re-partitioned and now I will have to reinstall everything. I will try to
get some examples to you tomorrow. I will post struct.member and
struct->member

Bill
 
K

Keith Thompson

Bill Cunningham said:
If your idea of diagnostics is compiler warnings maybe I'm wrong but if
it compiles I don't consider it a big problem.
[...]

Yes, you're wrong.

If gcc (with appropriate options; see Richard Heathfield's
suggestion elsethread) gives you *any* diagnostic messages, whether
it calls them warnings or errors, you need to pay attention to them.
(There are times when some warnings can be ignored, but frankly I
think you lack the ability to recognize such cases.)

If you have code for which gcc produces warnings, don't even *think*
about doing anything other than fixing the problems in the code
that triggered the warnings. If you need help doing that, post
the *exact* code and the *exact* warnings.
 
B

Barry Schwarz

If your idea of diagnostics is compiler warnings maybe I'm wrong but if
it compiles I don't consider it a big problem. If you mean a debugger like
gdb, I'm not quite sure how to use it. I know the -g switch is needed with
gcc to compile with debugging symbols.

Why do you keep changing the subject. No one mentioned the word
debugger. Stick to a single issue.
 
B

Barry Schwarz

Show us an example of your success with this since your previous post
claimed you didn't understand structs.

[snip]

Ok I think I have structs now. I will post it soon. My HD had to be
re-partitioned and now I will have to reinstall everything. I will try to
get some examples to you tomorrow. I will post struct.member and
struct->member

We have already established that struct->member is a constraint
violation so don't bother posting anything like that.
 
N

Nick Keighley

You should know that the OP has mental incapabilities!

He claims. But he doesn't seem to take steps to help himself.
Notes, flash cards etc. he doesn't copy the code as given but
makes "small" changes without understanding what he is doing.

He knows of
that fact and has declared it as he was very new to this group.

but he is no longer new to the group

That
means that his memory likes to forget what he has learned, he learns -
but he learns slowly, really slowly and needs to repeat steps more
often than you may think of. We have already accepted his handicaps at
all.

not all of us have accepted his handicaps.

Please be pationt with him. He does his best - even as this is not
much.

English is not my mothers language - and I'd no chance at scool to
learn it (learning in a pub from scottish peoples while drinking beer
is not the best choice

if it was glasgow then you're doomed... :)
 
N

Nick Keighley

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").

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

where "clean compile" means no errors and no warnings.
Bill, write that down. Stick it on your fridge.
 
N

Nick Keighley

I don't think he knows how. It seems to me (and I could be completely
mistaken about this) that BC lacks (or has to only a very limited
degree) the ability to recognise structure and organisation. If I am
right, he sees C as one great confusing lump of facts that are all
stirred together - he tries structs and finds they don't seem to
solve his problem, so he goes for precedence instead, and when that
doesn't solve his problem he has a go at recursive descent, or
strcoll(), or maximum munch, or __LINE__ - it's all just kind of
mixed up in one place. At least, that's the impression I get from his
articles.

Until he can learn (and remember) how to organise and compartmentalise
information in his head - something most of us had already more or
less worked out in our own heads before we ever began programming -
he's going to continue to come up with these random free
associations.

which may mean he'll never learn to program
 
B

Bill Cunningham

which may mean he'll never learn to program

I may not or it will take me awhile. But I will keep trying until I'm
layed to rest. I *will* master C. If I have to change to another languge
like java I will still learn to program. I am certainly not an algorithm
person. Not yet anyway. If anyone gets bored or agravated with me they can
certainly killfile me. No hard feelings. I am going to post soon a program
using struct and member pointers and members such as with the . operator.

Bill
 
H

Herbert Rosenau

He claims. But he doesn't seem to take steps to help himself.
Notes, flash cards etc. he doesn't copy the code as given but
makes "small" changes without understanding what he is doing.



but he is no longer new to the group

True, but as his capabilities are really small, he needs mutliple
tries to get a small step onwards.
not all of us have accepted his handicaps.

It is fine if anybody who can accept his disabilities will iignore
him. I observe him now a lot of month - and see that he does his best
to learn and that his tries are succeeds, very, very slow, but success
is visible.
if it was glasgow then you're doomed... :)

No, it was in germany.


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
B

Bill Cunningham

Barry Schwarz said:
Show us an example of your success with this since your previous post
claimed you didn't understand structs.

[snip]

Ok I think I have structs now. I will post it soon. My HD had to be
re-partitioned and now I will have to reinstall everything. I will try to
get some examples to you tomorrow. I will post struct.member and
struct->member

We have already established that struct->member is a constraint
violation so don't bother posting anything like that.

I don't know what constraint viollation means but here is what I have. I
first post the errors that occured when trying to use pointers and the
correct code that compiled file with c.member. This might be what you mean
when you say struct->member.

p.c:21: parse error before '->' token
p.c:25: parse error before string constant
p.c:25: warning: conflicting types for built-in function `printf'
p.c:25: warning: data definition has no type or storage class
#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);
}
struct Cat p;
struct Cat *pp;
struct Cat *pp = &p;
pp->name = "Striper";
pp->color = "Egyptian mau";
pp->age = 4;

printf("%p %p %p\n", pp->name, pp->color, pp->age);
}

Bill

No warning about constraint violations and no extra switches were used
like -ansi -W -Wall -pedantic and there's no __STRICT_ANSI__ defined
anywhere.

Bill
 
B

Barry Schwarz

snip


I don't know what constraint viollation means but here is what I have. I

It means that the code in question violates a constraint placed on the
language by the standard. The -> operator requires the left operand
to be a pointer. You have repeatedly stated you have code where the
operand is a structure. That is a constraint violation.
first post the errors that occured when trying to use pointers and the
correct code that compiled file with c.member. This might be what you mean
when you say struct->member.

p.c:21: parse error before '->' token
p.c:25: parse error before string constant
p.c:25: warning: conflicting types for built-in function `printf'
p.c:25: warning: data definition has no type or storage class
#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);
}

This is the end of main.
struct Cat p;
struct Cat *pp;
struct Cat *pp = &p;

These three lines are declarations and are allowed to appear at file
scope (outside of any function).
pp->name = "Striper";
pp->color = "Egyptian mau";
pp->age = 4;

printf("%p %p %p\n", pp->name, pp->color, pp->age);

All of these lines are statements and must appear inside a function.
This is a constraint violation which requires a diagnostic.

You use %p three times. Each %p requires the corresponding argument
to a pointer. The first two corresponding arguments are pointers. The
third is an int. Attempting to print an int with %p invokes undefined
behavior. This is just as bad as a constraint violation but the
compiler is not required to issue a diagnostic. Yours went the extra
mile and did.
No warning about constraint violations and no extra switches were used

There are at least three diagnostics related to constraint violations.
like -ansi -W -Wall -pedantic and there's no __STRICT_ANSI__ defined
anywhere.

Are you bragging about how you ignored previous advice? Why?
 
B

Bill Cunningham

Are you bragging about how you ignored previous advice? Why?

I am not bragging about it. I don't know what it is. You mentioned
constrait violation in your post and I was looking for the compiler to tell
me about a constrait violation and it didn't.


so for the pp->name; for example this is the right way isn't it,

printf("%p\n",pp->name);
rather than
printf("%s\n",pp->name); ?

I was a little torn there and didn't know which to use as I am familiar with
%p being used in a printf and printf prints the address pointed to.

Bill
 
N

Nick Keighley

    I am not bragging about it. I don't know what it is.

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).
You mentioned
constrait violation in your post and I was looking for the compiler to tell
me about a constrait violation and it didn't.

so for the pp->name; for example this is the right way isn't it,

printf("%p\n",pp->name);
rather than
printf("%s\n",pp->name);  ?

I was a little torn there and didn't know which to use as I am familiar with
%p being used in a printf and printf prints the address pointed to.

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.
 
B

Bill Cunningham

Bill Cunningham said:
I am not bragging about it. I don't know what it is. You mentioned
constrait violation in your post and I was looking for the compiler to
tell me about a constrait violation and it didn't.


so for the pp->name; for example this is the right way isn't it,

printf("%p\n",pp->name);
rather than
printf("%s\n",pp->name); ?

I was a little torn there and didn't know which to use as I am familiar
with %p being used in a printf and printf prints the address pointed to.
Ok I see my first choice was correct. My gut choice as this page from a
short tutorial shows. I should've used %s and %d instead of %p. Why is this
below not a constraint violation?

Pointers to structures
When you use a pointer to a structure you must use -> instead of a dot.

#include<stdio.h>

typedef struct person
{
char *name;
int age;
} PERSON;

int main()
{
PERSON p;
PERSON *pptr;
PERSON pptr = &p;
pptr->name = "John Smith"; <here and
pptr->age = 25; < here.
printf("%s",pptr->name);
printf("%d",pptr->age);
return 0;
}

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

Keith Thompson

Bill Cunningham said:
Ok I see my first choice was correct. My gut choice as this page from a
short tutorial shows. I should've used %s and %d instead of %p. Why is this
below not a constraint violation?

By "this below" do you mean the following paragraph or the code?
Pointers to structures
When you use a pointer to a structure you must use -> instead of a dot.

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?
#include<stdio.h>

typedef struct person
{
char *name;
int age;
} PERSON;

int main()
{
PERSON p;
PERSON *pptr;
PERSON pptr = &p;

You've declared pptr twice with different types. Your compiler
complained about it, and you foolishly ignored its complaints.
This error could cause the compiler to become confused and produce
incorrect diagnostics for the rest of the code.

I won't waste my time analyzing the rest of your code. Either fix
it so it doesn't produce any diagnostics, or post the code with
the diagnostics and ask us for help in fixing them.

When fixing code to remove diagnostics, it's usually best to start
with the first reported problem. Diagnostics after the first can
be unreliable.
 

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