Thread: Structures problem

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    14

    Structures problem

    Code:
    #include <stdio.h>
    
    void grade(struct student_info *);
    
    int main()
    {
    
    		struct student_info
    			{
    				int examno;
    				char name[25];
    				int marks;
    				char grade;
    			};
    
    		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++ )
    		{
    		    grade(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)
    		p->grade = 'F';
    				else
    				{	if (p->marks < 60)
    							p->grade = 'D';
    					else
    					{
    							if (p->marks < 70)
    								p->grade = 'C';
    							else
    								if (p->marks < 80 )
    									p->grade = 'B';
    								else
    									p->grade = 'A';
    					}
    				}        
    
    }
    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. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    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. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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
    hello, internet!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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;
    	char grade;
    };
    and this is the definition that can go inside main():
    Code:
    struct student_info student[50];
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    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. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Unregistered
    Guest

    Thumbs down

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

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

  12. #12
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    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).
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM