out

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

#include <stdio.h>

int out(int n, char *buffer) {
if (n=(int)NULL) {
return puts(buffer);
}
if (n=1) {
return fputs(buffer,stdout);
}
if(n!=1||n!=(int)NULL) {
fprintf(stderr,"Usage error\n");
return 0;
}
}

The above code before I added the int casts just by a gut feeling didn't
work. By adding the int casts in front of the NULLs what have I really done?
I hope the wrote the right thing. The code before the int casts compiled
into an object file but the compiler through bash's stderr gave me warnings
that I needed casts. Have I corrected a portability problem. It's not good
to guess things even if it's right but to know what you did.

Bill
 
I

Ian Collins

Bill said:
#include <stdio.h>

int out(int n, char *buffer) {
if (n=(int)NULL) {

Why are you (attempting) to compare an int to a pointer? The above
assigns the null pointer to an int.

Why not just write if( n == 0 ) ?

Is this a troll?
 
V

vippstar

Why are you (attempting) to compare an int to a pointer? The above
assigns the null pointer to an int.

Why not just write if( n == 0 ) ?

Is this a troll?

As I said before, this Bill Cunningham person is a troll.
Yet a lot of people ignore me and continue to respond...
 
D

Default User

As I said before, this Bill Cunningham person is a troll.
Yet a lot of people ignore me and continue to respond...

Either a troll or completely unable to learn the language. It's a
complete waste of time to attempt a dialog.



Brian
 
B

Bill Cunningham

Ian Collins said:
Why are you (attempting) to compare an int to a pointer? The above
assigns the null pointer to an int.

It compiled. Without warnings. I don't what I did.
Why not just write if( n == 0 ) ?

I didn't know if 0 was portable or not.
Is this a troll?
I still do not know alot about C. It shows. I read and study and learn
sometimes slow, sometimes faster. As far as the comment "Is this a troll?" I
cannot make you think "This is not a troll or this is a troll." I cannot see
you nor you me. Posts about trolls or anything not C I find irrelevant. If
you decide to think I am a troll I certainly am sorry and hope you don't.

Bill
 
K

Keith Thompson

As I said before, this Bill Cunningham person is a troll.
Yet a lot of people ignore me and continue to respond...

We don't ignore you, we merely disagree. I don't believe he's a
deliberate troll. He's said he has some sort of learning disability
but is trying to learn C in spite of it. I've seen no reason to
disbelieve him.

If you don't have the patience to answer his questions, that's quite
understandable. If you believe he is a deliberate troll, that's also
quite understandable. But some of us are inclined to try to help,
wisely or not.
 
K

Keith Thompson

Bill Cunningham said:
#include <stdio.h>

int out(int n, char *buffer) {
if (n=(int)NULL) {
return puts(buffer);
}
if (n=1) {
return fputs(buffer,stdout);
}
if(n!=1||n!=(int)NULL) {
fprintf(stderr,"Usage error\n");
return 0;
}
}

The above code before I added the int casts just by a gut feeling didn't
work. By adding the int casts in front of the NULLs what have I really done?
I hope the wrote the right thing. The code before the int casts compiled
into an object file but the compiler through bash's stderr gave me warnings
that I needed casts. Have I corrected a portability problem. It's not good
to guess things even if it's right but to know what you did.

I'm afraid your gut feeling has failed you. If a piece of code
doesn't work without casts, it's vanishingly unlikely that it will
work with casts. The casts are likely to silence some error messages,
but it's very much like solving a problem with your car by putting
electrical tape over the warning lights on the dashboard; it looks
better, but the underlying problem is still there.

"=" is assignment. "==" is comparison.

NULL is a null *pointer* constant; it makes no sense to use it as
you're doing here.

What *exactly* is your ``out'' function supposed to do? What is the
meaning of the parameter ``n''? Write an English specification of the
function, perhaps something in the style of a man page. Then you
might be able to implement it (and/or we might be able to help).

It looks like you're trying to call either puts or fputs, with the
value of ``n'' controlling which one you call (the difference being
whether a new-line is appended to the output). I suggest that that's
not a useful thing for a function to do. I imagine that any code that
uses your ``out'' function would look like this:
out(0, "some message");
or like this:
out(1, "some other message");

I can't think of any reason why you wouldn't want to write this:
puts("some message");
or this:
fputs("some other message", stdout);
instead.
 
B

Bill Cunningham

What *exactly* is your ``out'' function supposed to do? What is the
meaning of the parameter ``n''? Write an English specification of the
function, perhaps something in the style of a man page. Then you
might be able to implement it (and/or we might be able to help).

It looks like you're trying to call either puts or fputs, with the
value of ``n'' controlling which one you call (the difference being
whether a new-line is appended to the output). I suggest that that's
not a useful thing for a function to do. I imagine that any code that
uses your ``out'' function would look like this:
out(0, "some message");
or like this:
out(1, "some other message");

I can't think of any reason why you wouldn't want to write this:
puts("some message");
or this:
fputs("some other message", stdout);
instead.
I thought NULL was a macro meaning '0'. I guess again I was wrong. Yes
you guessed correct. Out is really a useless function other than an excerise
in function writing. Using 1 or 0 depends on wether or not you want output
with or without '\n'. I have corrected it and it works now and does exactly
as designed. I was thinking comparing n to 0 in the if statements. I forgot
about ==. I have been using if and getting used to it quite abit now. The
tutorial I use (one of them anyway) says that while is C's simplest loop. I
haven't tried nested ifs but I think I can imagine what they do.

I am an amateur programmer and can't spend as much time with C as I'd
like. So I learn what I can. It will take as long as it takes and I've seen
C code that blows my mind in clc and I just can't read it at all..yet ;)

Bill
 
D

Default User

Keith Thompson wrote:

We don't ignore you, we merely disagree. I don't believe he's a
deliberate troll. He's said he has some sort of learning disability
but is trying to learn C in spite of it. I've seen no reason to
disbelieve him.

He's supposedly been learning C since 2002 or so, and has made no more
progress than this. If he's not a troll, then everyone needs to realize
that it ain't gonna happen.




Brian
 
M

Martin Ambuhl

Bill said:
#include <stdio.h>

int out(int n, char *buffer) {
if (n=(int)NULL) {
^^^^^^^^^^^
assigns a null-pointer-constant coverted to an int
to n. Since n is almost certainly zero now, the
condition is always false, so
return puts(buffer);
^^^^^^^^^^^^^^^^^^^
is dead code.
}
if (n=1) {
^^^^
assigns 1 to n, so the test is always true,
so
return fputs(buffer,stdout);
^^^^^^^^^^^^^^^^^^^^^^^^^^^
is always executed and

everything from here on is dead code.
if(n!=1||n!=(int)NULL) {
fprintf(stderr,"Usage error\n");
return 0;
}
}

The above code before I added the int casts just by a gut feeling didn't
work. By adding the int casts in front of the NULLs what have I really done?

Something very, very stupid.
 
R

Richard

Jack Klein said:
[snip]
You took a void pointer to RAM address 0 (a bogus, never-used
address, used only to indicate that a pointer is NOT properly
initialized yet), cast it to an int, and compared it to n.
Technically legal, but meaningless. Since your "n" is an int,
not a pointer, why did you try to compare it to a pointer?

You seem to have some misunderstanding of the concept of the NULL
macro and null pointers in C.

A null pointer is not a "pointer to RAM address 0". This is true, if
for no other reason, because there are architectures that do not have
RAM at address 0, and there are C implementations for these
architectures.

1. A null pointer in C is not a "pointer to RAM address 0", it is
instead a pointer containing a value that is guaranteed not to compare
equal to the address of any object or pointer.

What does a NULL pointer show up as when stored as a value in a sizeof
(ptr) block of bytes? How does this differentiate from all other valid
memory pointers (especially ones pointing to address 0)?

How many architectures have had a NULL pointer where the "value" is
anything other than binary 0?
 
H

Harald van Dijk

What does a NULL pointer show up as when stored as a value in a sizeof
(ptr) block of bytes? How does this differentiate from all other valid
memory pointers (especially ones pointing to address 0)?

On one implementation I have access to, a null pointer is stored as
0x55555555, while a pointer to address zero is stored as 0x00000000.
 
R

Richard

Harald van Dijk said:
On one implementation I have access to, a null pointer is stored as
0x55555555, while a pointer to address zero is stored as 0x00000000.

And the maximum address of the memory space is?

Does "if(!p)" work when p is your NULL?
 
W

Walter Roberson

Richard said:
What does a NULL pointer show up as when stored as a value in a sizeof
(ptr) block of bytes?

There isn't necessarily only one NULL pointer value -- not even
necessarily only one NULL pointer value for any particular pointer type.
How does this differentiate from all other valid
memory pointers (especially ones pointing to address 0)?

Implementation dependant. For example, on the system I use most
often, 0x80000000 is guaranteed not to point to valid memory
because on this system, having the high bit set on a pointer indicates
that the pointer is in kernel space, and the kernel space is designed
on this system not to have valid memory at relative kernel offset 0.
How many architectures have had a NULL pointer where the "value" is
anything other than binary 0?

"How many" is always a difficult question to answer, since so
many products are obscure or their details are trade secret.

If the question is rephrased in terms of whether there have been
any "common" architecutres in which a NULL pointer was represented
by a value that was other than binary zero, the answer (I have read)
is "Yes, x86 systems that did not use the 'flat memory model'
could have a null pointer for each segment."
 
R

Richard

There isn't necessarily only one NULL pointer value -- not even
necessarily only one NULL pointer value for any particular pointer type.


Implementation dependant. For example, on the system I use most
often, 0x80000000 is guaranteed not to point to valid memory
because on this system, having the high bit set on a pointer indicates
that the pointer is in kernel space, and the kernel space is designed
on this system not to have valid memory at relative kernel offset 0.


"How many" is always a difficult question to answer, since so
many products are obscure or their details are trade secret.

If the question is rephrased in terms of whether there have been
any "common" architecutres in which a NULL pointer was represented
by a value that was other than binary zero, the answer (I have read)
is "Yes, x86 systems that did not use the 'flat memory model'
could have a null pointer for each segment."

Note : I'm not trolling here. I'm just trying to find the "perfect
explanation" for the benefit of others. In too many places there is zero
or null :)-)) understanding of NULL not being the same as a zero
pointer.

I find it telling that a web site called "Everything you need to know
about C pointers" skirts this discussion.

http://boredzo.org/pointers/
 
H

Harald van Dijk

And the maximum address of the memory space is?

0xFFFFFFFF. There's no special reason why 0x55555555 would not be
addressable, just as there's no special reason why 0x00000000 would not
be addressable.
Does "if(!p)" work when p is your NULL?

Yes. p behaves as true when it points to address 0, and as false when it
is a null pointer / points to address 0x55555555.
 
R

Richard

Harald van Dijk said:
0xFFFFFFFF. There's no special reason why 0x55555555 would not be
addressable, just as there's no special reason why 0x00000000 would not
be addressable.

So what is the address pointer pointing to address 0x55555555 stored as
when stored in memory?
 
H

Harald van Dijk

So what is the address pointer pointing to address 0x55555555 stored as
when stored in memory?

As 0x55555555. What is a pointer to address 0x00000000 stored as on your
system?
 

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

Latest Threads

Top