# Thread: Structures problem

1. ## Structures problem

Code:
```#include <stdio.h>

void grade(struct student_info *);

int main()
{

struct student_info
{
int examno;
char name[25];
int marks;
};

struct student_info student[50], *p;
int num, i;
printf( "Program of structure with pointer.\n\n") ;
printf( "How many students ? :" );
scanf( "%d", &num );
for (i=0; i<num; i++ )
{
printf( "Enter information of student %d\n", i+1 );
printf( "Exam Number                :" );
scanf( "%d%*c", &student[i].examno );
printf( "Name                       :" );
scanf( "%[^\n]", student[i].name );
printf( "Total marks obtained of 100 :" );
scanf( "%d", &student[i].marks );
}
for ( p=student; p < student+num; p++ )
{
}
printf( "\n" );
printf( "Exam Number    Name             Marks obtained in class  Grade\n\n" );
for ( p=student; p < student + num; p++ )
{
printf( "%7d        %-25s %9d    %8c\n", p->examno, p->name,p->marks, p->grade );
}
return 0;
}

void grade(struct student_info *p)
{
if (p->marks < 50)
else
{	if (p->marks < 60)
else
{
if (p->marks < 70)
else
if (p->marks < 80 )
else
}
}

}```
Okay, the code above does not compiles (9 errors 1 warning).

And I solved it by moving the structures outside main()

Now my question is,
Why the structures must be declared outside?
is it a must to declare structure outside main?

I hope someone can explain it for me.

Thanks.

(BTW, most of the code isn't mine, it's given in my school textbook)

2. >Why the structures must be declared outside?
>is it a must to declare structure outside main?
The structure must be defined before it can be used. A structure definition, like any other variable, has a scope, and in your example, the code requires that the struct is defined outside main to allow grade() to take it as a parameter.

Quite often you'll find that struct definitions go at the top of the source file, or in the header file.

3. First, no, you do not need to declare structures outside function main. In order for your structures to work, you need logical code.

I noticed many typos as well as logic errors in your code.

I recommend you step away from the computer with a piece of paper and think out the process you want. Start with the big picture, then break it down into routines.

Once you have a logical process and logical routines, thoughtfully write them out in psuedo code.

THEN go to the computer and write your functions CAREFULLY. Any compiler will try to compile and execute exactly what you wrote.

Good luck.

4. Code:
```C:\WINDOWS\Desktop>gcc -c dum.c -o dum.o -Wall
dum.c:3: warning: `struct student_info' declared inside parameter list
dum.c:3: warning: its scope is only this definition or declaration,
dum.c:3: warning: which is probably not what you want.
dum.c: In function `main':
dum.c:33: warning: passing arg 1 of `grade' from incompatible pointer type
dum.c: At top level:
dum.c:44: warning: `struct student_info' declared inside parameter list
dum.c:45: conflicting types for `grade'
dum.c:3: previous declaration of `grade'
dum.c: In function `grade':
dum.c:46: dereferencing pointer to incomplete type
dum.c:47: dereferencing pointer to incomplete type
dum.c:49: dereferencing pointer to incomplete type
dum.c:50: dereferencing pointer to incomplete type
dum.c:53: dereferencing pointer to incomplete type
dum.c:54: dereferencing pointer to incomplete type
dum.c:56: dereferencing pointer to incomplete type
dum.c:57: dereferencing pointer to incomplete type
dum.c:59: dereferencing pointer to incomplete type```
as hammer said, it is a scope problem. declaring the struct in main only allows you to use it in main

5. OK, to clarify, I used the word "define" when I should have used "declare".

I *should* have said:
The structure must be declared before it can be used as a parameter to the grade() function.

definition and declaration are two different things:
K&R2
Definition refers to the place where the variable is created or assigned storage

Declaration refers to places where the nature of the variable is stated but no storage is allocated.
Therefore, this is the declaration which should go outside main():
Code:
```struct student_info
{
int examno;
char name[25];
int marks;
};```
and this is the definition that can go inside main():
Code:
`struct student_info student[50];`

6. First, no, you do not need to declare structures outside function main. In order for your structures to work, you need logical code.
The reason people declare them outside of functions is so that other functions can use them.

Quzah.

7. So what I get from your replies is,
Structures are to be declared outside main() when other function need access to them.
Otherwise, they may be declared inside main().

(Correct me if i'm wrong.)

Thanks everyone.

8. I *should* have said:
The structure must be declared before it can be used as a parameter to the grade() function.
I disagree here. I think that your(Hammer) initial reply was right.
A structure declaration is when an instance of that structure is declared.

The structure definition is when the structure is initially defined, ie. where it is stated what the members of that type(struct) would be.

To me it sounds logical.

9. Originally posted by The Dog
I disagree here. I think that your(Hammer) initial reply was right.
A structure declaration is when an instance of that structure is declared.

The structure definition is when the structure is initially defined, ie. where it is stated what the members of that type(struct) would be.

To me it sounds logical.
It might sound logical, and that alone is the reason for my wording in my first post. But then I consulted the gold old K&R2 bible, and found the correct definition was as per the quote in my last post. If you have the book, section 6.1 refers to this.

Here's a couple of other qoutes to assist:
Code:
```struct point {
int x;
int y;
};```
The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces.
<snip>
A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, given the declaration of point above:
Code:
`struct point pt;`
defines a variable pt which is a structure of type struct point.

10. u people are screwing up simple things!!!!!!!!

11. >u people are screwing up simple things!!!!!!!!
C is very simple, using C is far from simple.

>A structure declaration is when an instance of that structure is declared.
>The structure definition is when the structure is initially defined,
>ie. where it is stated what the members of that type(struct) would be.
You have it backward. The structure definition is when the instance is declared and memory is set aside. The declaration is when the specifics of the structure in general are created so that the compiler can create an instance of it.

-Prelude

12. Declaration and Definition _are_ different.

If you declare a structure inside of main(), it can only be used inside of main() because it falls within the scope of what main() code knows about. That's a local declaration.

If you declare a structure outside of main(), then it may be used by all other functions/procedures within that file. It's declaration scope is 'global'.

---

declaration scoping is compiler-dependant. And I personally think scattern declarations throughout the code is sloppy, unless necessary for some gain (performance or otherwise).

Popular pages Recent additions