Re: never heard anything like it in my life?

From: Howard (alicebt_at_hotmail.com)
Date: 01/22/04


Date: 22 Jan 2004 14:37:56 EST


"Jumbo @uko2.co.uk>" <pcr1000011<nospam> wrote in message
news:1074729603.279695@news.minx.net.uk...
> Want a laugh check out what this guy Ron Natalie said to me. pmsl:
>
> "Jumbo @uko2.co.uk>" <pcr1000011<nospam> wrote in message
> news:1074695100.709794@news.minx.net.uk...
> > Would you like a demonstration of how to assign to an array?
> > int intarr[1];
> > intarr[0] =256;
> >
> intarr[0] is NOT an array. It is an int that happens to be in an array.
> You can't assign arrays. They are the only data type in C++ that
> doesn't have assignment semantics.
>
> I don't know how to reply to this :-P
>
> AHA.

Sounds like just a difference in terminology to me. When Ron (and most
other progarmmers) talk about assigning "to an array", he means assigning to
the *entire* array, not to an individual member of an array. You obviously
talk about assigning to a member of an array using the same terminology.
It's a matter of semantics, not an argument of facts.

There is no "assignment semantic" for arrays. In other words, the
assignment operator is not defined for the array type itself, only for its
members or for pointers to arrays. You can assign values to the members of
arrays, or to pointers to array data, but not assign the array *directly*.
For example, this is illegal:

int a[3] = {1,2,3};
int b[3];
b = a; // not valid!

But of course, you *can* write

for (int i = 0; i < 3; ++i)
  b[i] = a[i];

However, in the normally accepted terminology for programming, the latter is
not "assigning to an array", but rather "assigning to the members of an
array". A subtle diffrence, but it is important to be precise in this
field.

You can also write:

int* a = new int[3]; // initialize pointer with address of first element of
dynamically-allocated array data
int* b; // uninitialized pointer
b = a; // assign a to b (copying the address stored in a, not the data
pointed to by a)

But again, that is not assigning to an array, it is assigning to a pointer,
and it simply makes b and a point to the same memory location, and it is
that (unnamed) location which holds the array itself.

Many of us wish that C++ was designed so that you *could* do a direct
assignment to the entire array, as in my first example. That would have
made things a lot easier. But it's not how the C++ standard was written, so
we're stuck with it. And in order to say (in English) that such a thing
cannot be done, the only way to phrase that fact is to say "you cannot
assign to an array". (How else would you phrase it, in order to be precise
and not get your statement confused with assigning to a member of an array
or a pointer to an array, which *are* legal?)

So Ron's correct, just more careful (specific?) in his terminology than you
like to be. Give him a break, huh? He's been a great help to a lot of
people here, and he knows what he's talking about.

Regards,
    Howard

(p.s., what's "pmsl" in your post mean?)



Relevant Pages

  • Re: Malloc code
    ... malloc() not return NULL? ... returned from malloc to an array of pointers, and then return this array of ... So I just returned a void pointer and assign the value to ... TCP_LOAD_MCB_X and I am assigning the values to the items of the strucutre ...
    (microsoft.public.vc.language)
  • Re: Malloc code
    ... David Wil. ... your right I can just free it in main since the pointer to that allocation is passed back to main. ... You have most probably noticed that I was using an array called "xxx" in TCP_LOAD_MCB_X and I am assigning the values to the items of the strucutre like this: ... If your problem was that you were assigning too much memory, why did did mallocnot return NULL? ...
    (microsoft.public.vc.language)
  • Re: Question about arrays in objects
    ... >>I want to create a 2d array as an object member. ... > handled in the constructor, you just can't use a plain old array. ... > that is where using a pointer would come in. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Youre appointed as Portability Advisor
    ... None of its behaviour is undefined by the Standard. ... to write to member A of a union and then read from member B, ... character type that treats the thing as an array of bytes. ... dereferencing a pointer to one ...
    (comp.lang.c)
  • Re: What type is a structure?
    ... > It's true that I was thinking of a pointer when I said an array wasn't ... > the array's elements to the pointer which points to its first element. ... But a struct is not an array. ... after the last member, typically done in the interest of performance ...
    (comp.lang.c)