上午, 我希望有人在這裏 假設我有以下的結構,甚至更好結構的數組的組成員爲零
for (counter = 0; counter < 10; ++counter)
memset(bar[counter],0,sizeof(char)*10);
struct foo {
int a;
char b[10];
char c;
};
struct foo* bar;
bar = (struct foo*) malloc(sizeof(struct foo)*10);
memset(bar, -1, sizeof(struct foo)*10);
,而不是如何將在所有數組中的所有char/b成員數組中將b成員設置爲0?
基本上我的問題是類似於this一個
'sizeof(char)* 8'?爲什麼? '8'從哪裏來? – AnT
對不起,是的,這只是我的錯誤 –
你似乎正在用C編寫代碼。讓你的生活更輕鬆,並使用C++,例如'std :: vector'。那麼你將不必玩任何'malloc'和'memset' shenanigans。 –
juanchopanza