C array declaration and assignment?

前端 未结 7 1519
囚心锁ツ
囚心锁ツ 2020-11-29 07:59

I\'ve asked a similar question on structs here but I\'m trying to figure out how C handles things like assigning variables and why it isn\'t allowed to assign them to eachot

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 08:24

    In an attempt to complement Blank's answer, I devised the following program:

    localhost:~ david$ cat test.c
    #include 
    #include 
    int main (int argc, char * argv [])
    {
      struct data {
        int c [2];
      } x, y;
      x.c[0] = x.c[1] = 0;
      y.c[0] = y.c[1] = 1;
      printf("x.c %p %i %i\n", x.c, x.c[0], x.c[1]);
      printf("y.c %p %i %i\n", y.c, y.c[0], y.c[1]);
      x = y;
      printf("x.c %p %i %i\n", x.c, x.c[0], x.c[1]);
      printf("y.c %p %i %i\n", y.c, y.c[0], y.c[1]);
    
      return 0;
    }
    

    When executed, the following is output:

    x.c 0x7fff5fbff870 0 0
    y.c 0x7fff5fbff860 1 1
    x.c 0x7fff5fbff870 1 1
    y.c 0x7fff5fbff860 1 1
    

    The point is to illustrate how the copy of structures' values occurs.

提交回复
热议问题