Copying objects and arrays

R

Richard Herring

Brad Herald said:
Course webpage (http://courses.cs.vt.edu/~cs1704/fall03/)

I know this instructor who put this together agrees concisely with other
professors as to the validity of the notes below.

Argumentum ad numerum.
I know he has programmed
in C++ for many years as well as his colleagues.

Argumentum ad verecundiam.
I learned a lot of C++
from this instructor.

Argumentum ad misericordiam ;-)
Perhaps if Mr. Stroustrup were to visit and correct a misperception about
arrays and pointers.

Anyway, an excerpt (below) is from the course notes at the following link.

http://courses.cs.vt.edu/~cs1704/fall03/Notes/C03.Pointers.pdf

Pointer Expressions

Arrays == Pointers

Is simply wrong, unless "==" means "can be converted to".
- Non-indexed Array variables are considered pointers in C

"are considered [under some circumstances]" does not equate to "are".
- Array names as pointers contain the address of the zero
element (termed the base address of the array).

That "as pointers" rather begs the question.

Why not simply and accurately say, as does ISO/IEC 14882 4.2/1,

'An lvalue or rvalue of type "array of N T" can be converted to an
rvalue of type "pointer to T". The result is a pointer to the first
element of the array.'

?
 
M

Matteo Settenvini

A[10] is a pointer to 10 ints in memory (at least the space is assigned),

Wrong, A[10] is an array of 10 ints in memory. it is not a pointer.
&A[0] would be a pointer to a single int (which happens to be
the first int in an array). &A would be a pointer to an array of
10 ints.

Yes, I meant "right it's contiguous", not "right is a pointer".
No you don't. ITYM that you get a compilation error when
you assign two arrays (which is correct).

Whoops, I really was thinking "arrays" but I wrote "pointers" instead. My fault.
Yes, this is a feature of structs that when they get assigned,
array members get copied by value properly. This feature wasn't in
early drafts of C, which did not allow structs to be assigned,
but was added for 1989 ANSI C. Unfortunately they did not
also decide to allow assignment for standalone arrays.

Ok, now I got it, thanks!
 

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