【发布时间】:2023-03-18 21:05:02
【问题描述】:
我正在尝试使用 C 中的结构,但我被困在这一点上。这是我的代码:
#include <stdio.h>
void Test(void);
void updateIt(struct Item* ptr);
struct Item
{
double value;
int unitno;
int isTa;
int quant;
int minQuant;
char name[21];
};
int main(void)
{
Test(); // here I am gonna call updateit() function and print
}
void Test(void) {
struct Item I = { 100.10,100,10,110,10,"NAME!" };
updateIt(&I);
}
void updateIt(struct Item* ptr){
struct Item I[0] = 200 // This doesn't work — but why?
}
如何通过访问updateIt 函数内的值将Item I = { 100.10,100,10,110,10,"NAME!" } 的值更新为{ 200.20,200,20,220,20,"NAME2!"}?
【问题讨论】:
-
请注意,对于 C99 复合文字,您可以编写:
*ptr = (struct Item){ 200.20, 200, 20, 220, 20, "NAME2!"};为updateIt()函数的参数指向的结构分配一个新值。