如何正确使用C的结构数组

发布于 2025-02-11 12:45:28 字数 847 浏览 2 评论 0原文

我最近开始学习C语言的结构。我尝试了一个示例程序来扩展我的学习曲线。但是,在这个主题上,我面临的错误很少。任何人都可以找出以下程序中的错误。

#include<stdio.h>
main() {
 int i;
 struct elements {
  int z; /* Atomic Number */
  float m; /* Mass Number */
  char *name;
  char *symbol;
 };
 struct elements e[5];
 e[0] = (struct elements){1,1.008,"Hydrogen","H"};
 e[1] = (struct elements){2,4.0026,"Helium","He"};
 e[2] = (struct elements){3,6.94,"Lithium","Li"};
 clrscr();
 for(i=0;i<3;i++) {
  printf("Element Name: %s\n",e[i].name);
  printf("Symbol: %s\n",e[i].symbol);
  printf("Atomic Number: %d\n",e[i].z);
  printf("Atomic Mass: %0.2f\n",e[i].m);
 }
 getch();
 return 0;
}

这显示以下错误消息:

”在此处输入图像说明”

I'm recently started learning about structures in C Language. I tried out a sample program to extend my learning curve. But, here in this subject, I'm facing few errors. Shall anyone please figure out the errors in the following program.

#include<stdio.h>
main() {
 int i;
 struct elements {
  int z; /* Atomic Number */
  float m; /* Mass Number */
  char *name;
  char *symbol;
 };
 struct elements e[5];
 e[0] = (struct elements){1,1.008,"Hydrogen","H"};
 e[1] = (struct elements){2,4.0026,"Helium","He"};
 e[2] = (struct elements){3,6.94,"Lithium","Li"};
 clrscr();
 for(i=0;i<3;i++) {
  printf("Element Name: %s\n",e[i].name);
  printf("Symbol: %s\n",e[i].symbol);
  printf("Atomic Number: %d\n",e[i].z);
  printf("Atomic Mass: %0.2f\n",e[i].m);
 }
 getch();
 return 0;
}

This shows the following Error messages:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2025-02-18 12:45:28

尽管有其他评论,但如果您必须使用的话,那么您仍然需要解决方案。

尝试:

struct elements e[5] = {
    {1,1.008,"Hydrogen","H"},
    {2,4.0026,"Helium","He"},
    {3,6.94,"Lithium","Li"}
 };

您将在以后的编译器上使用的内容(尽管有些人希望char const *namesymbol接受字符串文字中),但实际上它的阅读不是很漂亮,当您定义整个数组时,也不需要它。

如果这样做,则可以省略数组大小(更改[5] to []),它将根据提供的元素数量进行大小。

Despite the other comments, if this is what you have to work with, then you'll still be wanting a solution.

Try:

struct elements e[5] = {
    {1,1.008,"Hydrogen","H"},
    {2,4.0026,"Helium","He"},
    {3,6.94,"Lithium","Li"}
 };

What you had would work on later compilers (although some will want char const * in the struct for name and symbol to accept string literals), but its actually not very pretty to read, nor is it necessary when you are defining the entire array.

If you do it this way, you can omit the array size (change [5] to []) and it will size according to the number of elements provided.

薔薇婲 2025-02-18 12:45:28

您不使用标准C编译器,因此无法用标准C语言编写代码。

您必须求助于1989年版的C语言,即“ C89”/“ C90”。它不支持复合文字特征(struct Elements){1,1.008,“ hydrogen”,“ h”};,因为该> ,因为该>> >语言23年前。

You aren't using a standard C compiler so you can't write the code in the standard C language.

You'll have to resort to the 1989 version of the C language known as "C89"/"C90". It doesn't support the compound literal feature (struct elements){1,1.008,"Hydrogen","H"};, because that one was added to the C language 23 years ago.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文