B
buuuuuum
why array can't be assigned, like structs?
why array can't be assigned, like structs?
why array can't be assigned, like structs?
[...]dwks said: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.
Because an array can be passed to a function,
or something, and the
compiler can not always know how big the array is.
why array can't be assigned, like structs?
[email protected] said:why array can't be assigned, like structs?
(e-mail address removed) wrote, On 08/03/07 05:26:
Because the language does not allow it.
and
why? what's the reason? why array's cant be like any other type in C?
why it have to be different?
im asking this because i can't find any good reason for this behavior,
since array is just another type of the language
K&R1 was published in 1978. In that book BWK notes that DMR's compilersantosh said: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.
Mark McIntyre said:An array isn't a type. Its a derived type, and has different
behaviour. Similarly structs.
[...]why? what's the reason? why array's cant be like any other type in C?
why it have to be different?
Keith Thompson said:Mark McIntyre said: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".
If you want to be able to copy arrays, put them in a struct and copy thewhy array can't be assigned, like structs?
Ben Pfaff said:Keith Thompson said:Mark McIntyre said: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.
Ian said: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;
}
Exactly, it shows the price you have to pay for a feature. To be ablesantosh said:Ian said:[email protected] said:why array can't be assigned, like structs?
If you want to be able to copy arrays, put them in a struct and copy the
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;
}
But if you find yourself doing this just for the sake of copying
arrays, you might be better of redesigning your program.
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.
If you want to be able to copy arrays, put them in a struct and copy the
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;
}
sacrifice all the simplicity and elegance of C's arrays.
yes,
int a3[3];
int a5[5];
are different type, so, cant be assigned
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.
are you talking about:
int *ptr=malloc(sizeof(int) * 10);
where ptr is a pointer
or
int (*ptr)[10];
where we know the array size?
[snip]If you want to be able to copy arrays, put them in a struct and copy the
structs.
its simple, isn't?
so, why arrays can't do this?
sacrifice all the simplicity and elegance of C's arrays.
if arrays were assignable you still could write &array[0], isn't it
the same thing?
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.