带结构的C数组 - 不能改变变量

时间:2015-08-21 15:01:13

标签: c arrays struct

任何人都可以解释原因吗

all_leds[0].pattern = 3;

all_leds[1].pattern = 4;

什么都不做?

#include <stdio.h>

int main(void)
{
    struct Led
    {
      int pin;
      int pattern;
    };

    struct Led led_1 = {1, 1};
    struct Led led_2 = {2, 2};

    printf("%d\n", led_1.pattern);  // prints 1
    printf("%d\n", led_2.pattern);  // prints 2

    struct Led all_leds[2];
    all_leds[0] = led_1;
    all_leds[1] = led_2;

    printf("%d\n", led_1.pattern);  // prints 1
    printf("%d\n", led_2.pattern);  // prints 2

    all_leds[0].pattern = 3;
    all_leds[1].pattern = 4;

    printf("%d\n", led_1.pattern);    // prints 1 ????
    printf("%d\n", led_2.pattern);    // prints 2 ????

    return 0;
}

4 个答案:

答案 0 :(得分:2)

Led是一个值类型(与C中的所有类型一样;它没有像C ++那样的引用类型)所以当你说all_leds[0] = led_1;时,你复制结构将led_1中的值放入all_leds的第一个元素中。在此行之后,all_leds[0]led_1保持单独的值,彼此之间没有任何关联。修改一个不会修改另一个。

相反,您可以使用指向all_leds值的指针填充Led

struct Led * all_leds[2] = { &led_1, &led_2 };
// ...
all_leds[0]->pattern = 3;
all_leds[1]->pattern = 4;

答案 1 :(得分:1)

您正在将led_1led_2的值复制到struct数组中。如果你想让它们成为相同的对象你应该让你的数组成为一个指向结构的指针数组,那么你可以通过引用更新它们。

#include <stdio.h>

int main(void)
{
    struct Led
    {
      int pin;
      int pattern;
    };

    struct Led led_1 = {1, 1};
    struct Led led_2 = {2, 2};

    printf("%d\n", led_1.pattern);  // prints 1
    printf("%d\n", led_2.pattern);  // prints 2

    struct Led *all_leds[2];
    all_leds[0] = &led_1;
    all_leds[1] = &led_2;

    printf("%d\n", led_1.pattern);  // prints 1
    printf("%d\n", led_2.pattern);  // prints 2

    all_leds[0]->pattern = 3;
    all_leds[1]->pattern = 4;

    printf("%d\n", led_1.pattern);   
    printf("%d\n", led_2.pattern);    

    return 0;
}

答案 2 :(得分:0)

好的,我想出了这个,它按预期工作:

#include <stdio.h>

int main(void)
{
    struct Led
    {
      int pin;
      int pattern;
    };

    struct Led led_1 = {1, 1};
    struct Led led_2 = {2, 2};
    printf("%d\n", led_1.pattern);
    printf("%d\n", led_2.pattern);

    struct Led * all_leds[2];
    all_leds[0] = &led_1;
    all_leds[1] = &led_2;

    printf("%d\n", led_1.pattern);
    printf("%d\n", led_2.pattern);

    all_leds[0] -> pattern = 3;
    all_leds[1] -> pattern = 4;
    printf("%d\n", led_1.pattern);
    printf("%d\n", led_2.pattern);

    return 0;
}

答案 3 :(得分:0)

在这些陈述中

DateTime dt = new DateTime();
  DateTime year2000 = dt.withYear(2000);
  DateTime twoHoursLater = dt.plusHours(2);

您正在输出结构printf("%d\n", led_1.pattern); // prints 1 ???? printf("%d\n", led_2.pattern); // prints 2 ???? led_1的数据成员。但是在这些陈述中

led_2

您更改了数组all_leds[0].pattern = 3; all_leds[1].pattern = 4; 的元素。数组和对象all_ledsled_1是不同的对象,占用不同的内存区域。因此,更改一个对象不会影响其他对象。

也许你的意思是以下

Led_2

或者你可以定义一个指向结构的指针数组。例如

printf("%d\n", all_leds[0].pattern);    // prints 1 ????
printf("%d\n", all_leds[1].pattern);    // prints 2 ????

在这种情况下的陈述后

struct Led *all_leds[2];
*all_leds[0] = &led_1;
*all_leds[1] = &led_2;

all_leds[0]->pattern = 3;
all_leds[1]->pattern = 4;

你会得到预期的结果。