我需要以某种方式存储关于某些分辨率的大量信息,我想为此制作一个结构。

struct resolution{
    const char* name;
    const int width;
    const int height;
};

问题是我需要在一些结构数组中存储很多分辨率,但无法正常工作(需要将126个内容存储到“手动”中,因此没有循环或类似的内容)。
谢谢!

最佳答案

你试过初始化程序吗?

struct resolution {
  const char * name ;
  const int width ;
  const int height ;
}
  mylist[] =
  {
    { "cga", 320, 240 },
    { "vga", 640, 480 },
    { "xga", 1024, 768 },
  } ;

作为奖励,您可以在末尾存储一个sentinel,这样读取这些内容的任何循环都可以在值上停止,而不必跟踪列表的长度:使用类似于:
{ NULL, 0, 0 }

因此,您可以检查nameNULL还是大小,如下所示:
const struct resolution *  find_bysize( int w, int h )
{
  struct resolution * searchptr ;

  for (searchptr= mylist ; ( searchptr-> name ) ; searchptr ++ )
    { if (( searchptr-> width == w ) && (searchptr-> height == h )) { return searchptr ; } }
  return NULL ;  // not found
}

09-04 00:49