2015-08-21 30 views
1

任何人都可以解釋爲什麼數組c與結構 - 不能改變的變量

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; 
} 
+1

您正在分配'all_leds [0] .pattern'並打印'led_1.pattern'那些是兩個不同的內存空間... –

+0

這是對某些事情的陳述。這不符合你的期望。 C具有價值語義。說'a = b;'意味着'a'具有與'b'相同的值,並不意味着它們都指向同一個對象。 – juanchopanza

+0

如果只在數組中存儲指向這些結構體的指針,那麼它會很有幫助嗎? –

回答

2

Led是值類型(如在C的所有類型,它沒有參考類型,如C++有),所以當你說all_leds[0] = led_1;你是複製led_1結構值成all_leds第一元件。在此行之後,all_leds[0]led_1保持獨立的值,彼此之間沒有任何連接。修改一個不會修改另一個。

取而代之,您可以使用指向Led值的指針填充all_leds

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

您要複製led_1led_2值放入你的結構數組。如果你希望它們是相同的對象,你應該使你的數組​​成爲指向結構體的指針數組,然後你可以通過引用來更新它們。

#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; 
} 
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; 
} 
+0

很多人同時在此工作:)謝謝大家 –

0

在這些語句

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

你ouputing結構led_1led_2的數據成員。然而在這些陳述中

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

您更改了數組all_leds的元素。數組和對象led_1Led_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; 

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

後,你會得到預期的結果。