[英]Assigning value to variable from struct array
我的頭文件中有一個結構數組,我在主文件中編寫了一些代碼。
char temp, index;
for (index = 0; index < 5; index++)
temp = cards[index].foo;
我注意到 temp 獲取第一個字符的值,但之后它不會改變; 即,如果結構數組中的第一個字符是“c”,那么它會得到“c”,但如果第二個字符是“f”,它就不會得到“f”——它會留在“c”。
我怎樣才能解決這個問題?
我的結構數組在我的標題中,看起來像這樣
struct x{
char foo;
} cards[Size];
嗨,兄弟,我剛剛在 C 中嘗試了您的代碼,它似乎可以正常工作.... 我的頭文件是
#include<stdio.h>
#define Size 5
struct x
{
char foo;
} cards[Size];
主要是:
#include <stdio.h>
#include <stdlib.h>
#include"my.h"
int main()
{
int i,index;
char temp;
for (i = 0; i < 5; i++)
cards[i].foo = 'a' + i;
for (index = 0; index < 5; index++)
{
printf("c[%d].foo = %c\n", index, cards[index].foo);
temp = cards[index].foo;
printf("t = %c\n", temp);
}
}
輸出是:
c[0].foo = a
t = a
c[1].foo = b
t = b
c[2].foo = c
t = c
c[3].foo = d
t = d
c[4].foo = e
t = e
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.