Thread: defining struct variable without using typedef

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    defining struct variable without using typedef

    when i define a new variable called temp
    i get
    missing ';' before 'type'

    why??
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    
     
    struct line {
    	char name[21], address[21], ID[10];
    	int grade;
    	int code;
    };
    
    
     
    int main ()
    {
    FILE* c[2];
     struct line  temp;
    
    c[0]=fopen("c:\\fl1.txt","r");//vote files ,each file represents the votes from a college
    c[1]=fopen("c:\\fl2.txt","r");
    struct line temp;
    
    fclose(c[0]);
    fclose(c[1]);
    
    
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Which line generates the error?

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    And I'm not 100% sure but could "line" be a reserved word?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you declare two variables named temp in the main function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    FILE* c[2];
     struct line  temp;
    
    c[0]=fopen("c:\\fl1.txt","r");//vote files ,each file represents the votes from a college
    c[1]=fopen("c:\\fl2.txt","r");
    struct line temp;
    Perhaps that one? [No, I didn't spot it, but gcc is very thorough and at least in my case gave good indication of what was wrong].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    And I'm not 100% sure but could "line" be a reserved word?
    It is not. See below - Laserlight got there a tad before me.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Missed that...

    By the way, please learn to properly indent your code. Very hard to read otherwise.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by laserlight View Post
    Why do you declare two variables named temp in the main function?
    ohh thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  5. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM