Thread: Please help for theis code

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    1

    Please help for theis code

    Hey,
    I am trying to run a program in which i use struct.It will take information form user of students then show it on screen.here is the code and I am getting this error.
    "error:'student' undeclared(first use in this function)"
    Header is at the last..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include "header.h"
    
    
    int main()
    {
        int i ;
        int a ;
        char h ;
        printf("How many students info you want to store:");
        scanf("%d", a);
        for(i=0;i<a;i++){
    
    
            struct info student[i] ;
    
    
            printf("\nEnter his first name:");
            scanf("%s", student[i].fname);
            printf("\nEnter his last name:");
            scanf("%s", student[i].lname);
            printf("\nEnter his id number:");
            scanf("%d", student[i].id);
            printf("\nEnter his class:");
            scanf("%d", student[i].clas);
            printf("\nEnter his age:");
            scanf("%d", student[i].age);
    
    
        }
        printf("\nDo you want to see students info?(y for yes or n for no):");
        scanf("%c", toupper(h));
    
    
        if(h == "Y"){
            for(i=0;i<a;i++){
    
    
                    printf("\n%d Student is %s %s", i, student[i].fname, student[i].lname);
                    printf("\nHis id number is %d", student[i].id);
                    printf("\nHis class is %d", student[i].clas);
                    printf("\nHis age is %d", student[i].age);
    
    
            }
    
    
        }else{
            printf("\nId return exit status 1.");
        }
    
    
        return 0;
    }
    //This is the header
    struct info{
    
    
       int id ;
       int clas ;
       int age ;
       char fname[25] ;
       char lname[25] ;
    
    
    };

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You declared student[] inside the scope of the for() loop, so it won't be visible outside that loop body.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The error you are seeing is a result of scope. You declare "students" in the scope of the first "for()" loop, so it is not accessible beyond that block. Therefore, later code does not see this variable, hence the error you saw.

    That is not the biggest problem, though.

    Code:
    for(i=0;i<a;i++){
    
        struct info student[i] ;
    
        // ...
    }
    You are trying to declare an array within a loop, which is most certainly not what you want.

    One way to fix both of these things is to declare the struct array with the rest of your variables, outside of all blocks (except within the main function, of course). I would also advise against using variable-length arrays (i.e. reading a number from the user into a variable and using that variable as the size of your array), unless you're compiling w.r.t. C99 and your compiler supports it. I'd recommend instead using a fixed-size array of sufficient size, and making sure the user input does not exceed this value. (You could also dynamically allocate memory for the exact size of the required array, but I assume this goes beyond your current level of understanding).

    After that, make sure you compile with max warnings. Here's some of what I got (note I moved your struct declaration above "main()" instead of including it as a header):

    Code:
    /*
    main.c||In function 'main':|
    main.c|13|warning: format '%d' expects type 'int *', but argument 2 has type 'int'|
    main.c|25|warning: format '%d' expects type 'int *', but argument 2 has type 'int'|
    main.c|27|warning: format '%d' expects type 'int *', but argument 2 has type 'int'|
    main.c|29|warning: format '%d' expects type 'int *', but argument 2 has type 'int'|
    main.c|34|warning: format '%c' expects type 'char *', but argument 2 has type 'int'|
    main.c|37|warning: comparison between pointer and integer|
    main.c|37|warning: comparison with string literal results in unspecified behavior|
    main.c|41|error: 'student' undeclared (first use in this function)|
    main.c|41|error: (Each undeclared identifier is reported only once|
    main.c|41|error: for each function it appears in.)|
    ||=== Build finished: 3 errors, 7 warnings ===|
    */
    This is the problem with writing a bunch of code all at once - errors and mistakes begin to pile up. Instead, you should be starting small and writing only a bit of code, compiling, fixing and testing. Then add a little bit more code, compiling, fixing, test. And so on.

    A development process

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM