Statically defined

Statically defined

Post by zlatoza » Sun, 20 Nov 2005 22:06:35


Hi all,

See code please:

...........
main {
char X[4]; /* statically defined */
X = "Jim"; /* ERROR */
...........

Would you explain me, why if X is statically defined aggregate type (a
struct or an array), then I can NEVER say:

X = somthing;

However I can say:

somthing = X;

Best regards,
Zlatozar
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

Statically defined

Post by Barry Schw » Wed, 23 Nov 2005 07:40:23


An array cannot appear to the left of the assignment operator. In
this case, you would need to use strcpy or something equivalent.

This limitation does not apply to all aggregates. A struct can appear
on the left of an assignment operator.


If X is an array, then in an expression like this it is evaluated as
the address of the first element of the array with type pointer to
element type. This value is no longer an aggregate. As long as
somthing is of the same or compatible pointer type, the statement is
syntactically correct.

If X is a struct, then somthing must be a struct also since struct
assignments are legal, as described above.


<<Remove the del for email>>
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

 
 
 

Statically defined

Post by Willer » Wed, 23 Nov 2005 07:40:34


Because X isn't an lvalue.


Because somthing is an lvalue.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

Statically defined

Post by Kalle Olav » Wed, 23 Nov 2005 07:40:56

"zlatozar" < XXXX@XXXXX.COM > writes:


Right, C doesn't let you directly assign to an array.
In this example, you could use strcpy instead.


You can assign to a variable of a struct type, even if the struct
contains an array.

int main(void)
{
struct {int i[1];} x, y;
y.i[0] = 0;
x = y;
return x.i[0];
}


If X is an array variable, then this assigns the address of its
first element. It does not copy the contents of the array.

There is also:

void f(int x[10])
{
x = x+1;
}

which is entirely equivalent to:

void f(int *x)
{
x = x+1;
}

and just increments the pointer, rather than assigns to an array.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

Statically defined

Post by Douglas A. » Wed, 23 Nov 2005 07:41:06


That's not "statically defined"; it's an auto variable.


Actually if X and somthing have structure type then assignment is allowed.


Not if somthing has array type.

What you seem to be missing is that arrays are not first-class objects in C;
in most contexts the name of an array variable is immediately converted to
a pointer to the element type and no longer represents the entire array
object
(although other elements can be reached through pointer arithmetic,
such as []-indexing). Further, this conversion produces a non-lvalue,
so the result cannot be assigned to. (I.e., you cannot make X point
anywhere
other than the first member of the declared array.)
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

Statically defined

Post by Jack Klei » Wed, 23 Nov 2005 07:41:25

On 19 Nov 2005 13:06:35 GMT, "zlatozar" < XXXX@XXXXX.COM > wrote in
comp.lang.c.moderated:


Objects inside a function are not statically defined unless you
specifically use the 'static' keyword. In this case, the array 'X'
has automatic storage duration, which is different from static storage
duration and different from allocated storage duration.


Correct, you cannot assign to an array. You can initialize it at the
point of its definition, however:

char X[4] = "Jim";

...is just fine.


Again, the example you show has noting to do with "statically"
defined. You seem to be using a different meaning for the word than
the C language does.

And your interpretation is incorrect, because you can assign values to
structs, which are an aggregate type:

#include <stdio.h>

struct example { int x; int y; };

int main(void)
{
struct example ex1 = { 3, 7 };
struct example ex2;

ex2 = ex1;
printf("ex2 = { %d, %d }\n",
ex2.x, ex2.y);
return 0;
}


Again, you can do this with structures, assuming that 'something' is
another structure of the same type. If you have a compiler that
supports the new C99 features, 'something' can even be a compound
literal, and not an actual object of the structure type.

But the language does not support assignment to arrays. That's just
the way it is.


Maybe you can do this, maybe you can't. If 'X' is an array and
'something' is either a pointer to the element type of the array, or a
pointer to void, then this is legal. This is due to an automatic
conversion of the name of an array to a pointer to its first element
in most expressions.

I think you should post again and explain what it is you mean when you
use the term "statically allocated", because in C language terms that
is not what your code snippet does.

--
Jack Klein
Home: http://www.yqcomputer.com/
FAQs for
comp.lang.c http://www.yqcomputer.com/ ~scs/C-faq/top.html
comp.lang.c++ http://www.yqcomputer.com/ ++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.yqcomputer.com/ ~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

Statically defined

Post by usr.roo » Wed, 23 Nov 2005 07:41:32

zlatozar ?

because x is a const pointer,you can not do that
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.