Typedef and Struct

This is a discussion on Typedef and Struct within the C Programming forums, part of the General Programming Boards category; Hi, I am doing a little work and I have this. Code: #include <stdio.h> #include <stdbool.h> #define NumberOfStudents 10 struct ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    62

    Typedef and Struct

    Hi, I am doing a little work and I have this.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    #define NumberOfStudents 10
    
    struct Student
    {
    	int studentID;
    	float balanceOwed;
    	float gpa;
    };
    
    typedef struct Student student;
    
    int main( int argc, char **argv )
    {
    	int	i;
    	int	id;
    	int	index = -1;	
    	bool	willGraduate = true;
    
    	Student student[]={
                {10, 0.0, 1.0},
                {20, 100.0, 3.0},
    	    {30, 0.0, 3.5},
    	    {40, -25.0, 3.25},
    	    {50, 0.0, 3.75},
    	    {60, 1000.0, 1.5},
    	    {70, 0.0, 3.25},
    	    {80, -50.0, 3.0},
    	    {90, 0.0, 3.9},
    	    {100, 1.0, 4.0}
    	}
    
    	
    	printf( "Enter student ID: " );
    	scanf( " %d", &id );
    
    	
    	for( i = 0; i < NumberOfStudents; i++ )
    	{
    		if( id == student[i].studentID )
    		{
    			break;
    		}
    	}
    
    	
    	if( i >= 0 )	
    	{
    		
    		if( student[i].balanceOwed > 0.0 )
    		{
    			willGraduate = false;
    			printf( "  Student owes ($%0.2f)\n",
    					student[i].balanceOwed );
    		}
    
    		
    		if( student[i].gpa < 2.0 )
    		{
    			willGraduate = false;
    			printf( "  This student has a low GPA (%.2f)\n", student[i].gpa );
    		}
    
    		
    		if( willGraduate )
    			printf( "  This student WILL graduate\n" );
    		else
    			printf( "  and will NOT graduate\n" );
    	}
    	else	
    		printf( "  Sorry, student ID (%d) not found\n", id );
    
    	return( 0 );
    }
    Gives error here -->Student student[]={ of

    'Student' undeclared (first use in this function).
    (Each undeclared identifier is reported only once for each function it appears in.)
    syntax error before "student".

    Where have gone wrong?
    thanks

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    maybe in your typedef it should read

    typedef struct student student;

    its thinking "Student" and "student" are different. either caps or no caps


    (I think. I'm new at this but I would try starting with that.)

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    typedef struct Student student;
    /* ... */
    Student student[]={
    /* ... */
    student is not the same as Student.

    [edit] Adding colour took too long . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well the typedef gives Student the alias student, so:
    > Student student[]={
    Either:
    Code:
    	student student[]={
    Or you could also use:
    Code:
    	struct Student student[]={

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by swoopy View Post
    Either:
    Code:
    	student student[]={
    Or you could also use:
    Code:
    	struct Student student[]={
    Naming a variable the same name as a struct tag, while allowed, is not usually a good thing.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Why not? You don't have to remember two names.

    I do agree that it's best to always use structure tags or always use typedefs. You can do the latter like this:
    Code:
    typedef struct Student
    {
    	int studentID;
    	float balanceOwed;
    	float gpa;
    } Student;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by dwks View Post
    Why not? You don't have to remember two names.
    I don't mean you shouldn't name the typedef the same as the struct tag. I do that all the time. I mean, don't declare a VARIABLE which has the same name as the struct tag (or typedef). In the example:

    Code:
    student student[10];
    It's the repetition of "student", once as a typename, once as an identifier, that I dislike.

    EDIT: For one thing, it completely hoses cscope. You say, "search for definition" for "student" -- will it give you the variable definition, or the struct definition? Who the hell knows. Ran into this just the other day, even. Basic idea:

    Code:
    struct foo { ... };
    typedef struct foo foo;
    static foo foo;
    Where foo is a struct tag, a typename, AND a variable all in the same file. The first two are okay, it's that third line that bugs me.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    D'oh, yes, I dislike it when people do that as well. I misread your post.

    It's worth noting that stdbool.h and bools in general are C99, as well as declaring variables in the middle of blocks.

    You're missing a semicolon here.
    Code:
    	Student student[]={
                {10, 0.0, 1.0},
                {20, 100.0, 3.0},
    	    {30, 0.0, 3.5},
    	    {40, -25.0, 3.25},
    	    {50, 0.0, 3.75},
    	    {60, 1000.0, 1.5},
    	    {70, 0.0, 3.25},
    	    {80, -50.0, 3.0},
    	    {90, 0.0, 3.9},
    	    {100, 1.0, 4.0}
    	};
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    cj147 I appreciate that;

    dwks as always you teach;

    swoopy, brewbuck you both pointed out good programming practices for me.

    All bear with me, I am learning and I will do my best to incorporate everything.

    Thanks to all for being so fast with the replies!!!

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by brewbuck View Post
    Naming a variable the same name as a struct tag, while allowed, is not usually a good thing.
    Well if one's capitalized, and the other isn't, I don't see why not. Unless you're coding in a language where case doesn't matter.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by swoopy View Post
    Well if one's capitalized, and the other isn't, I don't see why not. Unless you're coding in a language where case doesn't matter.
    If case matters, then the two aren't the same any more and I won't complain. As much. My personal naming style forbids use of upper case altogether except in macros, so that can't happen.

    (That's PERSONAL style. What I do at work is totally different.)

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I do agree with you:
    Code:
    student student;
    is not a good idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21