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

12
  • 4
    You seem to be using TurboC which was ancient already when the C99 standard came, and added the features you are using. You just can't use compound literals in that old environment. There are a few very good modern C and C++ environments with modern compilers which can handle your code, even free ones. Please upgrade to something modern. Commented Jun 29, 2022 at 14:25
  • I really doubt Turbo C supports assignments using compound literals. Commented Jun 29, 2022 at 14:26
  • 1
    Don't use TurboC. This antiquated tool is older than you. If your teacher told you to use turbo c, he should be fired or the school should be closed alltogether Commented Jun 29, 2022 at 14:27
  • 2
    While I think the best suggestion would be to dump Turbo C and get some decent compiler, you could use standard array initializators instead of assignments: struct elemens e[5] = {{1,1.008."Hydrogen","H"}, {2,4.0026,"Helium","He"}, ... };. Commented Jun 29, 2022 at 14:28
  • 1
    Unless you are using this computer from 1989, you probably shouldn't be using a compiler from 1989 either. (And yes, that's only $8499 for this beauty, with the money value of year 1989!) Commented Jun 29, 2022 at 14:36

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.