why array can't be assigned, like structs?
array
Collapse
This topic is closed.
X
X
-
buuuuuum@gmail.comTags: None -
dwks
Re: array
On Mar 7, 10:26 pm, buuuu...@gmail. com wrote:Because an array can be passed to a function, or something, and thewhy array can't be assigned, like structs?
compiler can not always know how big the array is. (This is the only
reason I can think of offhand, there are almost certainly others.)
Structures, on the other hand, always contain the same contents and so
the compiler can assign them.
Of course, if you want to assign one array to another, you can use the
memcpy() function (or memmove() if the parameters might overlap):
int array[] = {1, 2, 3, 4, 5};
int toarray[sizeof(array)/sizeof(*array)];
memcpy(toarray, array, sizeof(array));
-
Keith Thompson
Re: array
"dwks" <kingwilliams@p rimus.cawrites:[...]On Mar 7, 10:26 pm, buuuu...@gmail. com wrote:>>why array can't be assigned, like structs?
Because an array can be passed to a function, or something, and the
compiler can not always know how big the array is. (This is the only
reason I can think of offhand, there are almost certainly others.)
Structures, on the other hand, always contain the same contents and so
the compiler can assign them.
No, an array can't be passed to a function. You can do what *looks*
like passing an array to a function, but you're really just passing a
pointer to its first element.
Arrays aren't first-class objects in C. "First-class" isn't a
well-defined concept, but basically it means that there are things you
can do with other types that you can't do with arrays. There isn't
necessarily some fundamental reason why this is so (it isn't in some
other languages); it's just the way the language happens to be
designed, influenced by its predecessors B and BCPL.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Comment
-
Flash Gordon
Re: array
buuuuuum@gmail. com wrote, On 08/03/07 05:26:Because the language does not allow it.why array can't be assigned, like structs?
--
Flash Gordon
Comment
-
Richard Heathfield
Re: array
dwks said:
No, it can't.On Mar 7, 10:26 pm, buuuu...@gmail. com wrote:>>why array can't be assigned, like structs?
Because an array can be passed to a function,
The compiler knows /exactly/ how big the array is.or something, and the
compiler can not always know how big the array is.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
email: rjh at the above domain, - www.
Comment
-
SRR
Re: array
On Mar 8, 10:26 am, buuuu...@gmail. com wrote:Because its the way language has been designed!why array can't be assigned, like structs?
Its a feature(or liability!) inherited from the langauage B, C's
predecessor.In B arrays are called Vectors
You dont have the concept of structures in B. They are concepts
developed by C and therefore Ritchie has allowed assignment of one
structure variable to the other.
Comment
-
santosh
Re: array
buuuu...@gmail. com wrote:It probably because of the very close relationship between arrays andwhy array can't be assigned, like structs?
pointers in C. Pointers can, and often do, point to arbitrarily sized
blocks of memory, which can't be assigned to one another by the
compiler.
In fact, in pre-ANSI C, even instances of structures could not be
automatically assigned to one another.
Comment
-
buuuuuum@gmail.com
Re: array
On Mar 8, 4:20 am, Flash Gordon <s...@flash-gordon.me.ukwro te:andbuuuu...@gmail. com wrote, On 08/03/07 05:26:
>>why array can't be assigned, like structs?
Because the language does not allow it.
why? what's the reason? why array's cant be like any other type in C?Because its the way language has been designed!
why it have to be different?
why? why pointer and arrays have this relationship? There is no need!It probably because of the very close relationship between arrays and
pointers in C. Pointers can, and often do, point to arbitrarily sized
blocks of memory, which can't be assigned to one another by the
compiler.
And why only arrays are like this, and structs, integers, chars are
not
im asking this because i can't find any good reason for this behavior,
since array is just another type of the language
(sorry about my english)
Comment
-
Mark McIntyre
Re: array
On 8 Mar 2007 11:01:30 -0800, in comp.lang.c , buuuuuum@gmail. com
wrote:
An array isn't a type. Its a derived type, and has different>On Mar 8, 4:20 am, Flash Gordon <s...@flash-gordon.me.ukwro te:>>buuuu...@gmail .com wrote, On 08/03/07 05:26:
>>>>why array can't be assigned, like structs?
>Because the language does not allow it.
>and
>>Because its the way language has been designed!
>why? what's the reason? why array's cant be like any other type in C?
>why it have to be different?
behaviour. Similarly structs.
Not in C.>im asking this because i can't find any good reason for this behavior,
>since array is just another type of the language
--
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
Comment
-
Joe Wright
Re: array
santosh wrote:K&R1 was published in 1978. In that book BWK notes that DMR's compilerbuuuu...@gmail. com wrote:>>why array can't be assigned, like structs?
It probably because of the very close relationship between arrays and
pointers in C. Pointers can, and often do, point to arbitrarily sized
blocks of memory, which can't be assigned to one another by the
compiler.
>
In fact, in pre-ANSI C, even instances of structures could not be
automatically assigned to one another.
>
implemented struct assignment. There was no Standard of course until
1989 and K&R1 does not describe it as part of C.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Comment
-
Keith Thompson
Re: array
Mark McIntyre <markmcintyre@s pamcop.netwrite s:No, an array type, like any derived type, is a type. For example,On 8 Mar 2007 11:01:30 -0800, in comp.lang.c , buuuuuum@gmail. com
wrote:
>>>>On Mar 8, 4:20 am, Flash Gordon <s...@flash-gordon.me.ukwro te:>>>>buuuu...@gmai l.com wrote, On 08/03/07 05:26:
>>>
>why array can't be assigned, like structs?
>>>
>>Because the language does not allow it.
>>and
>>>>>Because its the way language has been designed!
>>why? what's the reason? why array's cant be like any other type in C?
>>why it have to be different?
An array isn't a type. Its a derived type, and has different
behaviour. Similarly structs.
"int[4]" is a type, "array of 4 ints".
Both array types and structure types are derived types; assignment is
defined for structure types, but not for array types.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Comment
-
Keith Thompson
Re: array
buuuuuum@gmail. com writes:[...]On Mar 8, 4:20 am, Flash Gordon <s...@flash-gordon.me.ukwro te:>>buuuu...@gmail .com wrote, On 08/03/07 05:26:
>>>>why array can't be assigned, like structs?
>Because the language does not allow it.
and
>>Because its the way language has been designed!
why? what's the reason? why array's cant be like any other type in C?
why it have to be different?
Because it's difficult, in terms of language design, to support array
assignment and make it correct and useful.
Structure assignment is relatively easy. All structures of a given
type have the same size, so assignment can just copy the entire
content of the structure to the target object.
On the other hand, you can have arrays of different numbers of the
same element type. For example:
int a3[3];
int a5[5];
If you wanted to support array assignment, you could just say that a3
and a5 have different types and can't be assigned. Or you could allow
a subset of an array to be copied to a subset of another, but that
requires inventing a syntax to specify a subset of an array, something
C doesn't have. But it's also important to be able to deal with
dynamically allocated arrays, preferably using the same syntax. This
introduces the possibility of writing an array assignment where the
source and target lengths don't match, but the mismatch can't be
detected until run time. Now you have to decide what to do if the
lengths don't match. Do you copy just part of the array, ignoring the
excess? Or do you define some way of detecting the error at run time;
if so, should it terminate the program, or should there be a way to
recover after the fact? Now you need an exception handling mechanism,
something that few if any languages had when C was first being
designed. Or do you just say that a length mismatch causes undefined
behavior, introducing an incredibly rich new source of program bugs?
C's array semantics are relatively simple, and almost elegant in an
uncomfortably-close-to-the-hardware kind of way. By using pointers
(or, equivalently, array indexing), you can do anything with C arrays
that you can do with "real" arrays in any other language. You can
assign arrays using memcpy(), for example (and if the language
directly supported array assignment, the compiler would likely do the
equivalent of a memcpy() call anyway). It requires more work for the
programmer, but it also allows finer-grained control and flexibility.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Comment
-
Ian Collins
Re: array
buuuuuum@gmail. com wrote:If you want to be able to copy arrays, put them in a struct and copy thewhy array can't be assigned, like structs?
>
structs.
typedef struct array10 Array10;
struct array10 {
int data[10];
};
int main(void)
{
Array10 first = {{1,2,3,4,5,6,7 ,8,9,10}};
Array10 second = first;
return 0;
}
--
Ian Collins.
Comment
-
Ben Pfaff
Re: array
Keith Thompson <kst-u@mib.orgwrites :
I thought about responding similarly to Mark's article, but thenMark McIntyre <markmcintyre@s pamcop.netwrite s:>>An array isn't a type. Its a derived type, and has different
>behaviour. Similarly structs.
No, an array type, like any derived type, is a type. For example,
"int[4]" is a type, "array of 4 ints".
it occurred to me that, in fact, "array" isn't a type in the same
sense that, say, "int" is a type. "Array of int" (e.g.) is an
(incomplete) type, but "array" isn't really a type; it's more of
a declarator, to adopt name from the Standard's syntax
productions.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Comment
-
Keith Thompson
Re: array
Ben Pfaff <blp@cs.stanfor d.eduwrites:Good point, though I don't know whether that's what Mark meant (noteKeith Thompson <kst-u@mib.orgwrites :
>>>Mark McIntyre <markmcintyre@s pamcop.netwrite s:>>>>An array isn't a type. Its a derived type, and has different
>>behaviour. Similarly structs.
>No, an array type, like any derived type, is a type. For example,
>"int[4]" is a type, "array of 4 ints".
I thought about responding similarly to Mark's article, but then
it occurred to me that, in fact, "array" isn't a type in the same
sense that, say, "int" is a type. "Array of int" (e.g.) is an
(incomplete) type, but "array" isn't really a type; it's more of
a declarator, to adopt name from the Standard's syntax
productions.
that he said "an array", not "array".
An array type, such as int[4], is a derived type, and is a type.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Comment
Comment