Converting to/from pointer

R

Richard

Mark McIntyre said:
Here's the situation: a self-confessed novice posts a question.
Someone answers him, but he's confused by the answer. Does he say to
himself

You are obviously paranoid and hanging around in this NG for too long
has altered you for the worst.

You clearly thought he should know who Richard Heathfield is.

This is a help group. People pop in for help and leave. They are under
no obligation to hang around to discover that Flash Gordon and
CBFalconer keep a running tally on how many times they can tell people
they are "off topic".
a) hmm,I'm a novice, I have no clue, I'll reread those posts and see
what I missed.
or
b) hmm, I know have no clue but I'll still write a correction, cos I
don't like admitting I'm lost.

It read more to me like he really thought he knew the right answer. It
happens.
Imagine someone asked a question about relativity, and Albert Einstein
answered. Imagine if the questioner then started to correct Einstein's
answer based on his own (possibly incorrect) understanding. Wouldn't
that be hilarious?

No. It wouldn't. I would feel a little embarassed for the poster and
explain to him in other words how he is wrong and for why. And as I
said, the OP was neither rude nor disrespectful.

Richard Heathfield and yourself, are not Albert Einstein equivalents in
any shape or form. Sorry if you were lead to believe you were the
programming world's equivalent from posturing too long in this NG.

regards

Richard.
 
K

Keith Thompson

Malcolm McLean said:
Army1987 said:
Lame Duck said:
No, a pointer to a float is the same as an array of float.

#include <stdio.h>
int main(void)
{
float array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
float *pointer = array;

printf("%lu %lu\n", sizeof array, sizeof pointer);
return 0;
}

Displays "40 4" on my system. So? They're the same but one is ten
times bigger? :)
So there's a bug in the standard that breaks array pointer
equivalence? It happens to even the best languages.
Deprectate sizeof( object) and only allow sizeof(type) and the pro

Is that a joke?

It's not a bug. There is no equivalence between arrays and pointers,
nor is any such equivalence intended.

IMHO, there are some problems with the standard, but those are things
make it *appear* that arrays and pointers are equivalent.

We have enough trouble telling newbies that arrays and pointers are
entirely distinct concepts. Please don't add to the confusion.
 
K

Keith Thompson

Lame Duck said:
On 3 Jun 2007 at 20:33, Keith Thompson wrote: [...]
You have been advised repeatedly, and in many cases quite politely,
that your question is about C++, not about C, and that you should post
it in comp.lang.c++, not in comp.lang.c. And yet you persist in
posting to comp.lang.c.

No I'm posting this now in comp.lang.c++.
[...]

You are posting to both comp.lang.c and comp.lang.c++.

Read your newsreader's documentation and learn how to set the
Newsgroups: header properly.

Followups redirected to comp.lang.c++.
 
J

James Kanze

James Kanze said:
No, he didn't. He posted to clc. I tried to move the whole thing over to
clc++ by crossposting it there and setting followups, but someone
removed my followup suggestion.

Worse, then. If he'd have lurked for even the shortest time, he
should have noticed that std::vector wasn't an approved topic in
clc:).
 
M

Martin Ambuhl

Lame Duck ignorantly top-posted:
Somehow I doubt that - my instructor was a C++ expert who'd programmed
for more than one very big company.

There were two choicesYour "Somehow I doubt that" is completely clueless. If your instructor
was a C++ expert, then you misunderstood. If you did not misunderstand,
then your instructor was not a C++ expert. Your "Somehow I doubt that"
has no antecedent for "that". You cannot sanely doubt the choice.
 
J

Jack Klein

This is called a divergent thread. If you wish to ignore it then please
do. But while the link between C and C++ is being discussed it is
reasonably on topic

[snip]

No, it is not. The reason that it is not is that the C standard does
not define such a link. The C standard did not permit or condone the
adoption of certain keywords, operators, and functions from C into
C++. Or Java, Javascript, C#, Objective C, or however many other
"borrowers" there are.

The C standard mentions C++ exactly twice, in non-normative footnotes
added at the request of the C++ standard committee.
and I'm quite sure anyone discussing does not need
your permission or re-directions to other newsgroups - especially when
the C++ one would be as "on or off topic" as the C one when one
considers BOTH languages are being discussed.

comp.lang.c is an unmoderated group, so no discussion needs anyone's
permission. But off-topic is off-topic, and any discussion of a link
between C and C++ is indeed off-topic.

C++ defines a link to C. The reverse is not true.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
D

Default User

CBFalconer said:
Certainly not if you apply the errata, which eliminates the casting
of malloc :)

Even without that, it's not. K&R2 still uses implicit return types for
functions returning int, including every main().




Brian
 
K

Keith Thompson

Jack Klein said:
The C standard mentions C++ exactly twice, in non-normative footnotes
added at the request of the C++ standard committee.
[...]

Quibble: There are four such footnotes, plus the ban on predefining
the macro __cplusplus. This doesn't refute your point, of course.
 
N

Nick Keighley

OK, maybe in C there's some subtelty that I haven't worked out, but
definitely in C++ - one of the main "soundbite to remember" in my C++
class was "an array is just the same thing as a constant pointer".

how many times do you have to be told you are wrong before you
accept that you *are* wrong? Perhaps you could read the FAQ or
do some research? Read the standard perhaps?

What I meant was: a C struct is like a C++ class without a constructor
or destructor

yes but a C struct is not a C++ class, by any stretch of the
imagination.

If you had an array of floats then your original question is trivial.
If you have a vector of floats then go and ask the question in
comp.lang.c++.

Why are you using floats rather than doubles?
 
M

Mark McIntyre

You are obviously paranoid and hanging around in this NG for too long
has altered you for the worst.

Feel free to be offensive, its cheap and sounds impressive.
You clearly thought he should know who Richard Heathfield is.

Actually I didn't. I did however think it was amusing that he
'corrected' him.
This is a help group. People pop in for help and leave.

Since when did "asking for help" include correcting the advice you
were given? Do you normally correct your teacher?
They are under
no obligation to hang around to discover that Flash Gordon and
CBFalconer keep a running tally on how many times they can tell people
they are "off topic"

Apparently the concept of nettiquette has completely passed you by.
It read more to me like he really thought he knew the right answer. It
happens.

Absolutely. And I still maintain that its probably wiser for novices
to do some background checking on a topic and their teachers, before
shooting from the hip.
Richard Heathfield and yourself, are not Albert Einstein equivalents in
any shape or form. Sorry if you were lead to believe you were the
programming world's equivalent from posturing too long in this NG.

Don't be a bigger prat than you need to be. If you can't understand a
metaphor when it bites you on the toe....
--
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
 
R

Richard Heathfield

Nick Keighley said:
how many times do you have to be told you are wrong before you
accept that you *are* wrong?

Being told many times that one is wrong does not mean that one *is*
wrong. I've been told many times that I'm wrong, by people who don't
know C. Does that mean I must accept that I'm wrong? No, of course not.
A worse case: Marilyn vos Savant was told loudly and often by *very
skilled and experienced mathematicians* that her solution to the Monty
Hall problem was wrong - but she was still right. One of those very
mathematicians used much the same argument that you did - "how many
mathematicians must tell you you are wrong before you accept that you
are wrong?" (Undoubtedly a misquote, but a fair paraphrase
nonetheless).

Lame Duck is not wrong *because* lots of people have said so in Usenet.
He's wrong because the Standard says so.
Perhaps you could read the FAQ or
do some research? Read the standard perhaps?

A much better suggestion!
 
K

Kenneth Brody

CBFalconer said:
No it isn't.
[...]

Yes, it is. C is an "extended subset" of LISP, for sufficiently
large values of "extended" and sufficiently small values of "subset".

Ditto for any pair of languages.

(Note Mr. Tobin's use of "which of course covers everything".)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

Johan Bengtsson

Mark said:
Since when did "asking for help" include correcting the advice you
were given? Do you normally correct your teacher?
Perhaps not normally, but sometimes the teacher is wrong (not implying
that it was the case here, just as a general statement). I have on
occasion when I was at school corrected my teacher simply because he was
wrong. It does happen.
 
R

Richard Tobin

OK, maybe in C there's some subtelty that I haven't worked out, but
definitely in C++ - one of the main "soundbite to remember" in my C++
class was "an array is just the same thing as a constant pointer".
[/QUOTE]
That is wrong in both C++ and C. Either your instructor was grossly in
error or you misunderstood.

I think you overstate the case. It is not literally true that an array
is the same as a constant pointer, but the statement does capture the
most distinctive aspect of C's array mechanism as compared with that of
other programming languages.

(And even an uncritical student who never looks into the details of the
statement will in fact, if they use C regularly, know the most
important exception to it.)

The student who wrongly believes that a C array is "just the same
thing as a constant pointer" is better educated than one who merely
knows the mechanics of using C arrays.

-- Richard
 
R

Richard Tobin

Nevertheless, either he was mistaken or you misunderstood him. No matter
what his expertise, the language specification always trumps an
instructor.

Only if your aim is to have a comprehensive graps of the details of C.
I don't know what kind of class the poster was taking, but that aim
should be rather low on the list of priorities for a university
course.

-- Richard
 
M

Malcolm McLean

Richard Tobin said:
Only if your aim is to have a comprehensive graps of the details of C.
I don't know what kind of class the poster was taking, but that aim
should be rather low on the list of priorities for a university
course.
In this case the instructor is probably simplifying for pedagogical
purposes. You have to do that. You don't throw quantum physics at nine year
olds, but you can show them models made out of balls and sticks. One problem
is that they often get the idea that atoms are coloured. However that's
better than thinking that atoms are something that only clever people can
know anything about.

However university students are there to learn the principles of
programming, not to churn out industrial spec code. As long as they know
that there is a standard, and that if you adhere to it the code will be
portable

<RANT>
Except to Microsoft's new platform. They have broken my entire code base.
</RANT>

then that's all that really needs to be learned.
 
K

Keith Thompson

That is wrong in both C++ and C. Either your instructor was grossly in
error or you misunderstood.

I think you overstate the case. It is not literally true that an array
is the same as a constant pointer,[/QUOTE]

A synonym for "not literally true" is "not true".
but the statement does capture the
most distinctive aspect of C's array mechanism as compared with that of
other programming languages.

I strongly disagree. The distinctive aspect of C's array mechanism is
that *operations* on arrays are defined in terms of operations on
pointers.

Arrays are not pointers. Pointers are not arrays. Students who fail
to understand this are likely to make mistakes like:

void foo(int array[])
{
... sizeof array ...
/* expected to be the size of an array object, but it's really
the size of a pointer object */
}
(And even an uncritical student who never looks into the details of the
statement will in fact, if they use C regularly, know the most
important exception to it.)

The most important exception is that arrays are not pointers.

If you mean the most important exception to the "rule" that you can
use pointers as if they were arrays, there are several exceptions, all
of them important.
The student who wrongly believes that a C array is "just the same
thing as a constant pointer" is better educated than one who merely
knows the mechanics of using C arrays.

And the student who understands the truth is far better educated that
either of them.

The truth about arrays and pointers *isn't that hard to understand*.
It's only difficult because some instructors try to "simplify" it.

My standard response to anyone who's confused about this is:

Arrays are not pointers. Pointers are not arrays. Read section 6
of the comp.lang.c FAQ, <http://www.c-faq.com>.

Is that really so difficult that it's necessary to lie to students?
 
O

Old Wolf

The student who wrongly believes that a C array is "just the same
thing as a constant pointer" is better educated than one who merely
knows the mechanics of using C arrays.

I don't see how you can claim this. Say we
have an array of 100 ints on a typical system.

You think it is 'better educated' to believe
that an object of 400 bytes is the same thing
as an object of 4 bytes?

IMHO the student who knows correct information
about C arrays is better educated than one who
knows correct information and also some
dangerous untruths.

The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays. How many times a week
do we get people posting asking why they can't
assign their array of arrays to a char** ?
 
M

Mark McIntyre

A synonym for "not literally true" is "not true".

Point of information: this is itself not literally true.

Something not literally true can be figuratively true, or essentially
true, or largely true.

For example it is not /literally/ true that Chris Torek is a guru. At
least, I assume he's not a sikh or buddhist teacher.


(i make no comment on the pointer discussion, I'm just correcting an
incorrect correction....)
--
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
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top