I am trying to implement a structure in C. I'm getting these errors.
Please help me with this error and also explain to me what am I doing wrong.
main.c:7:12: error: variable ‘s1’ has initializer but incomplete type
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~~~~~~
main.c:7:26: warning: excess elements in struct initializer
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~~~~~
main.c:7:26: note: (near initialization for ‘s1’)
main.c:7:33: warning: excess elements in struct initializer
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~
main.c:7:33: note: (near initialization for ‘s1’)
main.c:7:36: warning: excess elements in struct initializer
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~
main.c:7:36: note: (near initialization for ‘s1’)
main.c:7:39: warning: excess elements in struct initializer
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~~~
main.c:7:39: note: (near initialization for ‘s1’)
main.c:7:20: error: storage size of ‘s1’ isn’t known
7 | struct student s1 = {"Nick",16,50,72.5};
My code
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct student s1 = {"Nick",16,50,72.5};
printf("%s",s1.name);
// return 0;
}
struct student{
char name[4];
int age;
int roll_no;
float marks;
}s1;
CodePudding user response:
In the point of this declaration
struct student s1 = {"Nick",16,50,72.5};
the compiler do not know how the structure is defined and what data members it has. So it issues the messages.
You need to place the structure definition before declaring an object of the structure type. For example
struct student{
char name[4];
int age;
int roll_no;
float marks;
};
int main( void )
{
struct student s1 = {"Nick",16,50,72.5};
//...
Pay attention to that you are trying to declare two objects with the name s1 of the structure type.
The first one is declared in main
struct student s1 = {"Nick",16,50,72.5};
and the second one in the file scope after main
struct student{
char name[4];
int age;
int roll_no;
float marks;
}s1;
Remove this declaration in the file scope and place the structure definition before main as it was shown above.
CodePudding user response:
A C compiler reads each source file from the top down. So at the time you attempt to create a variable of type struct student
, that type hasn't been defined yet.
Move the struct definition to the top of the file before it is used.
CodePudding user response:
As posted in the comments, you have a few problems with the code:
- When you try to use
student
thestruct
is not yet known by the compiler char[4]
is too short to hold "Nick" since there is a terminatingNULL
in every string and in order to usename
with%s
in aprintf()
you need this terminating0
.
I will show you an example with a few common ways to declare and use this
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char name[15];
int age;
int roll_no;
float marks;
} Student;
int main()
{
Student one;
Student other = {"Nick", 16, 50, 72.5};
Student modern_way =
{
.age = 12,
.name = "James Bond 007"
};
one = other; // one had no value yet
Student a_group[3] =
{
[1] = one,
[0] = other,
[2] = modern_way
};
printf("%s, %d\n%s, %d\n",
one.name, one.age,
other.name, other.age
);
printf("%s, %d\n", modern_way.name, modern_way.age);
// now a loop to list the ones in the array
for (int i = 0; i < 3; i = 1)
printf("-: %s, %d\n",
i,
a_group[i].name,
a_group[i].age);
return 0;
}
output
Nick, 16
Nick, 16
James Bond 007, 12
0: Nick, 16
1: Nick, 16
2: James Bond 007, 12
In the code you see that is far more convenient to create a typedef
Student
so you can use it without having to remember and retype struct
all over the code.
Also useful is the convention to use the first letter in uppercase for the named things.
one
is declared but not initialized, but has a value assigned later in the code
modern_way
is initialized in the not so new 90's way where you can name the fields and initialize only the ones you need to, and in any order.
a_group
shows you how to deal with initializing an array and show that you can even use other known values in the code.
A for
loop shows how to list them all.
CodePudding user response:
put it this way
#include<stdio.h>
#include<stdlib.h>
struct student{
char name[4];
int age;
int roll_no;
float marks;
}s1;
int main()
{
struct student s1 = {"Nick",16,50,6};
printf("%s",s1.name);
// return 0;
}