且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ç迭代结构数组

更新时间:2023-10-18 16:49:28

有关编译器将接受初始化语法(这应该是任何标准的C编译器),你应该能够这样写:

For compilers that will accept the initializer syntax (that should be any standard C compiler), you should be able to write:

struct mystruct
{
  char a[10];
  double b;
};  // semi-colon added!

struct mystruct array[20] =
{ 
   { "test1", 1.0 },  // character strings!
   { "test2", 2.0 },
};
enum { ARRAY_SIZE = sizeof(array) / sizeof(array[0]) };

int i;
for (i = 0; i < ARRAY_SIZE && array[i].a[0] != '\0'; i++)
{ 
    printf("[%s] => %f\n", array[i].a, array[i].b);
}