Why pointers??

R

Richard Heathfield

Frederick Gotham said:
Richard Heathfield posted:


#include <stdio.h>

Good guess, and well corrected.

You still have another outstanding error that needs correcting - an apology
to Keith Thompson. Don't leave it too long.
 
C

CBFalconer

Ancient_Hacker said:
Well, in C, it's hard to impossible to do certain things without
resorting to pointers.

If you can get by with arrays and structs and arrays of structs,
then go ahead.

array references are automatically converted to pointers in most
cases. Especially when passing an array as a function parameter.
So you can to all practical purposes eliminate the use of arrays
also. Now you need to name each component of your original array,
and dispense with indices. What fun.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
C

CBFalconer

August said:
.... snip ...

Raw pointers must be used with great care. With them we can for
instance write a function that returns a pointer to the stack:

int *f(void)
{
int t;

return &t;
}

This is not possible in a language with (only) restricted pointers.

And this is totally useless, since any dereference of the return
value of f results in undefined behaviour. All you can do with it
is compare it for equality with some other int pointer, and the
result should be false. With one possible exception.

int *p1, *p2;

p1 = f();
p2 = f();
if (p1 == p2) puts("possible, but useless");
puts("One example of undefined behaviour");
return 0;

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
M

Mark McIntyre

Why use pointers at all??

How else would you point to things?

(its a homework question, or else a stupid one, its like saying "why
use sausages at all?". )
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

CBFalconer

Frederick said:
.... snip ...

Your compiler is non-conforming if it refuses to compile the
following program:

#include <iostream>

int *Func()
{
int t;
return &t;
}

int main()
{
int i; std::cin >> i;

if(i) Func();
}

No, it is non-conforming if it compiles that collection of syntax
errors, undefined behaviour, etc.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
O

Old Wolf

CBFalconer said:
And this is totally useless, since any dereference of the return
value of f results in undefined behaviour. All you can do with it
is compare it for equality with some other int pointer, and the
result should be false.

Actually the return value is indeterminate:

6.2.4#2
The value of a pointer becomes indeterminate when
the object it points to reaches the end of its lifetime.

so you could be evaluating a trap representation (ie. UB) by
assigning it to a variable. I'm not sure whether:

f();

causes UB or not.
 
F

Frederick Gotham

Richard Heathfield posted:
You still have another outstanding error that needs correcting - an
apology to Keith Thompson. Don't leave it too long.

I can think for myself, thank you.

I do not deny the original allegation (in fact, I hereby confirm it). I
condemn the dragging up of old, forgotten business when the only gain is to
cause conflict and breed animosity.

Whether offence was intended or not, or whether offence was taken, is of
little consequence to me at this stage -- in fact, I've long forgotten the
argument.

In "real life", I might be inclined to settle little matters such as these
with dialogue, and other times I might decide that it's better to let
things silently blow over. Different resolutions suit different conflicts.
In internet discussions such as these however, I get a bit lost for words;
I might write a paragraph or two, but then realise that I'm assuming my
audience is of the same culture as mine, and so forth -- at which point I
resign myself to the fact that I simply don't understand how he or she
thinks. Without face-to-face contact, it can be hard to read the other
person.

I don't understand why such offense was taken to the word, "fascist". My
intent to was to poke at you, Kieth, expressing my condemnation of your
attitude to "char unsigned". The intent was to stir you, not to downright
offend you with offensive labels (e.g. such as "son of a bitch",
"shithead", etc.). If you were highly offended by this, then you must
understand that I am at a loss to understand why.

Finally: This is an old, forgotten topic. If I were to call everyone up on
everything bad they'd ever done to me, I wouldn't have any friends. Nor
family come to think of it.
 
R

Richard Heathfield

Frederick Gotham said:
I don't understand why such offense was taken to the word, "fascist".

Then it's time for you to study a little history, bonehead.
 
K

Keith Thompson

This is a point of personal privilege. This post contains no topical
material, except that it's indirectly relevant to the topic of
civility in this newsgroup.

Frederick Gotham said:
I don't understand why such offense was taken to the word, "fascist". My
intent to was to poke at you, Kieth, expressing my condemnation of your
attitude to "char unsigned". The intent was to stir you, not to downright
offend you with offensive labels (e.g. such as "son of a bitch",
"shithead", etc.). If you were highly offended by this, then you must
understand that I am at a loss to understand why.

My name is Keith, not Kieth. At least get that right. (It was
probably just an accidental typo, but I'm in no mood to be forgiving
right now.)

I am truly at a loss to understand why you are surprised that I would
take offense at being called a fascist. It was a stupid and offensive
thing to write. I would probably have been slightly *less* offended
if you had called me a "son of a bitch" or a "shithead"; I think of
those as more or less generic insults whose literal meaning is
irrelevant. By calling me a fascist, on the other hand, you
explicitly compared me to the likes of Benito Mussolini, Francisco
Franco, and Adolf Hitler.

And if you don't realize that that's what you did, you need to learn
what the word "fascist" really means.

Your provocation for this was that I criticized your coding style and
advised others not to emulate it.
Finally: This is an old, forgotten topic. If I were to call everyone
up on everything bad they'd ever done to me, I wouldn't have any
friends. Nor family come to think of it.

It's not that old; it was last month. I called you on it at the time,
and you refused to apologize then, when it was a brand new topic fresh
in your memory.

I again call on you to publicly apologize to me for your severe and
unjustified insult. Don't do it because you think I'm forcing you to
(I'm not, and I can't, and I wouldn't if I could). Don't do it because
you think it would keep you out of a few killfiles. Do it because
you were in the wrong, and apologizing is the right thing to do.
 
P

Peter Nilsson

CBFalconer said:
No, it is non-conforming if it compiles that collection of syntax
errors, undefined behaviour, etc.

No. The C standard does not _require_ a conforming implementation to
succeed or fail to compile the above code.

Conformance is generally limited to two things: issuing _at least
one_ diagnostic for constraint violations, and the correct semantic
implementation of strictly conforming programs (subject to
implementation limits.)

Code that is not strictly conforming may or may not require a
diagnostic. If it does, then a conforming compiler must issue it.

But whether such code is accepted, compiled, linked, etc... is
completely at the implementation's discretion. That is the freedom
that undefined behaviour allows.

C99 was the first C standard that actually put a requirement
on an implementation to fail to translate a translation unit. That
was solely for the case of the #error preprocessing directive.
 
O

Old Wolf

Peter said:
Conformance is generally limited to two things: issuing _at least
one_ diagnostic for constraint violations, and the correct semantic
implementation of strictly conforming programs (subject to
implementation limits.)

Code that is not strictly conforming may or may not require a
diagnostic. If it does, then a conforming compiler must issue it.

But whether such code is accepted, compiled, linked, etc... is
completely at the implementation's discretion. That is the freedom
that undefined behaviour allows.

It also has to compile conforming (but not strictly conforming)
programs that are don't have any syntax errors or constraint violations.
 
J

Jack Klein

Why use pointers at all??

Don't. That means all your code will need to be in your main()
function, because you can't call a function without using a pointer.
 
S

Stephen Sprunk

onkar said:
Why use pointers at all??

Any non-trivial program needs some sort of mechanism to refer to
non-local objects. In C, these things are called pointers. In newer
languages they are usually called references, because so many
programmers think "pointers" are evil, but they're roughly the same
thing. C makes it easy to shoot yourself in the foot via pointers;
other languages with "references" make it difficult to shoot yourself in
the foot but also make many other interesting and useful things
difficult in the bargain. Power comes with a price, as does safety.

S
 
L

lovecreatesbea...

onkar said:
Why use pointers at all??

If the arguments of a function should be modified then the addresses of
them should be passed into the function and the corresponding
parameters should be declared as pointers of type same as the arguments.
 
R

Richard Heathfield

(e-mail address removed) said:
If the arguments of a function should be modified

You can't modify a function's arguments. You can, however, modify the value
of a function parameter.
then the addresses of
them

You can't take the address of an argument. Arguments are expressions, and
expressions in the general case do not necessarily have addresses.
should be passed into the function and the corresponding
parameters should be declared as pointers of type same as the arguments.

What you're trying to explain is /how/ to use a pointer in a function call
to facilitate the modification of an object's value within that function,
but the question is /why/ you would want to do that.
 
F

Frederick Gotham

Richard Heathfield posted:
Then it's time for you to study a little history, bonehead.

Is it customary for one to announce their kill-filing of a fellow-poster?
 

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

Latest Threads

Top