Converting to/from pointer

K

Keith Thompson

CBFalconer said:
How many times do we have to tell you - C does not have vectors.
Please observe the follow-up and cease cross-posting to c.l.c and
c.l.c++.

Though this thread started with a question about C++ vectors, I don't
believe Francine was using the word in that sense.
 
F

Francine.Neary

Though this thread started with a question about C++ vectors, I don't
believe Francine was using the word in that sense.

Certainly not! In context I see why the word was an unfortunate choice
- to be honest this thread has gotten so far out of control that I'd
forgotten its murky origins :)

Let's try to analyze my abuses of language...

At this stage, I'm using "array" to cover two different concepts: a
bona fide C array, and a block of memory dynamically allocated by
malloc, which we can think of conceptually as an array even though it
isn't an array as far as C is concerned, and we need to be careful not
to burn our fingers with sizeof etc.
Pushing this one step further, identifying an ordered

Here we need to distinguish between 1-dimensional arrays and 2-
dimensional arrays, where some linguistic tangles are helpful... I
intended vector to be a synonym for array, where array has the loose
sense of the previous paragraph, and matrix to mean "2-dimensional
array, which is an array (in the loose sense above) of vectors (in the
sense just defined)". Of course, you could also take a matrix (in this
sense) to be a single array (in the loose sense above) and keep track
yourself of rows and columns in a single index.
 
N

Nick Keighley

A recent example [of problems arising from assuming an array
is a pointer]. This was proposed (simplified from actual code)
typedef struct
{
size_t size;
unsigned char data[0];
} Msg_t;

That's the "struct hack"; see question 2.6 in the comp.lang.c FAQ,
<http://www.c-faq.com/>. A more nearly portable version uses [1]
rather than [0].

yes, I know. That was what I proposed.

If you're going to use the struct hack, don't depend on sizeof
yielding anything meaningful.

um. Well sizeof(Msg_t) yields something meaningful. Somehow you have
to work out what value to supply to malloc().

The struct hack has the advantage of keeping the data in one piece --
which is why C99 added flexible array members to replace it.

my point was that the person who proposed this thought two versions
were
equivalent. Because "arrays are the same as pointers".


--
Nick Keighley

Your colleagues have been successfully briefed for years on
the progress of Special Projects without their ever having
any idea what they actually are.
 
M

Mark McIntyre

Yes. Obviously one of us is mistaken, and it is high time to work
out who that is and why.

Thank heavens for someone posting a sensible answer to the question...
At least one of us will learn something.
Normally it will be me.

:)
--
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
 
M

Mark McIntyre

If 'side of the earth' means half of the surface,
then Australia certainly is on the opposite side
to Britain.

But that's not how it's usually interpreted; [/QUOTE]

I disagree. In most of the UK "The other side of the earth" is taken
to mean just that.

--
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
 
M

Mark McIntyre

The word was "opposite", not "other".

Same response.
--
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
 
M

Malcolm McLean

Certainly not! In context I see why the word was an unfortunate choice
- to be honest this thread has gotten so far out of control that I'd
forgotten its murky origins :)
No. Obviously we cannot reserve the word "vector" for a particular C++
standard template library object.. It has got a perfectly acceptable meaning
as the opposite of a scalar.

Some regs seem to fall over this point quite frequently. Programming
languages are quite new and do grab certain words for special meanings.
However there is no point trying to avoid those words in other contexts, as
in my distinction between a "function", which calculates something, and a
"procedure" which does something. Unfortunately the C convention is to call
all subroutines "functions", but that is C stretching the English language,
not me.
 
C

Charlton Wilbur

MML> In this case the instructor is probably simplifying for
MML> pedagogical purposes. You have to do that.

Yes, but you must do it *responsibly.* You don't say "arrays and
pointers are the same thing"; you say "arrays and pointers are alike
in a lot of ways." It is substantially more difficult to unlearn
something wrong and relearn it correctly than it is to learn correctly
in the first place; any pedagogue who thinks he's doing the students a
favor by teaching them things that are oversimplified ought to be sued
for malpractice.

Charlton
 
J

Johan Bengtsson

Keith said:
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?
Some teachers are too stupid to understand this themself. Not
necessarily implying that this is the case here.
 
J

James Kanze

MML> In this case the instructor is probably simplifying for
MML> pedagogical purposes. You have to do that.
Yes, but you must do it *responsibly.* You don't say "arrays and
pointers are the same thing"; you say "arrays and pointers are alike
in a lot of ways."

You don't even say that, because it's just as wrong. Arrays and
pointers are not even vaguely alike. The only relationship is
that an array will implicitly convert to pointer in certain
circumstances (all of which are in an expression---outside of an
expression, there is no relationship whatsoever).

Note that in order to use an array or a pointer in an
expression, you must first declare or define one. In other
words, you must be able to use them in a context where they are
not at all related before using them in a context where the
implicit conversion creates an appearance of similarity.
 
A

Army1987

Johan Bengtsson said:
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.

And usually the teacher answers "I did it on purpose, I wanted to
check wheter you were paying enough attention"... :)
 
A

Army1987

Default User said:
A fair number of astronomers would consider your "correction" to be
incorrect. Many consider the Earth/Moon system to be a double planet,
based on the relative sizes and proximity of the two bodies.

But the center of mass of the system is well below Earth's
surface...
 
A

Army1987

If 'side of the earth' means half of the surface,
then Australia certainly is on the opposite side
to Britain.

But that's not how it's usually interpreted; if it was, the Philipines
and Falklands would also be on the opposite side of the earth from
Britain.

It is interesting to see how little land there is on the hemisphere
centred on the antipodes of London:

http://www.garwood-voigt.com/catalogues/H19362PolessouthThomson.jpg[/QUOTE]

see also http://en.wikipedia.org/wiki/Image:WorldMapWithAntipodes.png
 
A

Army1987

Malcolm McLean said:
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.

"An array always evalues ("decays") to a constant pointer to its
first element, except as the operand of sizeof or of the unary &.
For example, given:
int arr[100];
int * const ptr = &arr[0];
Every expression containing arr will have the same identical value
that it would have if arr were replaced with ptr, unless arr is
used as the operand of & or sizeof."

Too complicated for students? If so, you'd better teach those
students some other language.
 
A

Army1987

Barry Schwarz said:
On 2007-06-04 17:00:59 -0700, "Malcolm McLean" <[email protected]>
said:

snip


It's not complex, it's non-existent. Functions cannot accept arrays as
parameters.

Of course they can. Or are you referring to the fact that the
argument evaluates to a pointer so the function never really sees it
as an array (but nevertheless has no trouble treating it as an array)?

And the syntax is only two characters more complex than passing a 1D
array.

void func(int[][N], int);
int array[M][N] = {...};
func(array, M);

Only if N is a compile-time constant.
 
A

Army1987

Barry Schwarz 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);

It would be better to cast the last two arguments so someone whose
system did not #define size_t as unsigned long would not invoke
undefined behavior.

Yes, I've forgotten to do that. (But on my system size_t is
unsigned long, so my point still holds.)
 
K

Keith Thompson

Army1987 said:
"An array always evalues ("decays") to a constant pointer to its
first element, except as the operand of sizeof or of the unary &.
For example, given:
int arr[100];
int * const ptr = &arr[0];
Every expression containing arr will have the same identical value
that it would have if arr were replaced with ptr, unless arr is
used as the operand of & or sizeof."

Too complicated for students? If so, you'd better teach those
students some other language.

You forgot one case: a string literal used to initialize an array
doesn't decay to a pointer.
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top