Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unused variable

Status
Not open for further replies.

DocHaydn

Programmer
Feb 24, 2004
12
CA
Hello,
I'm a learner. Compiler: MinGW Developer Studio, O/S:Win'98SE.

The program is a mail manager, taken from my "C"
book:"C by Example". On compilation, I'm getting the
warning:"Unused variable'list'"; on the line:"struct
mail_struct list[MAX];", just after,int main(){.

This variable is not used anywhere else in the book;
in my program, so I'm stumped. If I remove it, I get
the error:"syntax error before token."

It seems as though, it's a use,or remove issue.
How do I safely remove this variable; or where in my
program do I use it?

I'll only post the offending part of the program.

Thank you.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>



/*Global structure of name and address.*/

struct mail_struct
{
   char name[25];
   char address[25];
   char city[20];
   char province[20];
   char zipcode[7];        /* Extra NULL character.*/
   char code [10];         /*For additional expansion.*/

};

/*Global file pointer.*/

FILE *fp;

/*Defined constants.*/
/*MAX is the total number of names allowed in memory for reading
mailing lists.*/

#define MAX 250
#define FILENAME "ADDRESS.DAT"

/*Prototype all of the program's functions.*/

int main(void);
void disp_menu(void);
void enter_name(void);
int change_name(void);
void print_name(void);
void flush_newline(char *line);
void add_to_file(struct mail_struct item);
char pr_data(struct mail_struct item);
void get_new_item(struct mail_struct *item);
void pause_sc(void);
int getzip(char zipcode[]);


/**********************************************************/

int main()
{
   struct mail_struct list [MAX]; /*Warning is here.*/
   int ch;

   do
   {
      disp_menu();
      scanf(" %d", &ch);
      while(getchar()!='\n');

      switch(ch)
      {
         case(1) : {enter_name();
               break;}
         case(2) : {change_name();
               break;}
         case(3) : {print_name();
               break;}
         case(4) :  {break;}
         default :  {printf("You need to enter");
                     printf(" 1 through 4");
                     break;}
    }
  }
  while(ch != 4);
  getchar();

  return 0;
}
 
Dunno - I can remove it quite happily and it compiles as expected.

--
 
Doesn't the "syntax error before token" tell you which token it's complaining about? It it complaining about the int ch; line?

You can probably get rid of the "unused variable" error by just typeing: list; after you declare it, but since it's not used you should really remove it.
 
I think you made some other changes and they're making the compiler to fail. Repeat the process and you'll get the errors away.

Cheers,
Dian
 
Unused/unreferenced variable is a warning, not an error. The list var does not referenced in main(), it's true. You may discard this var definition in main() safety.
But if you remove struct mail_struct type declaration too, your compiler will detect an error (like described in the original post) because of struct mail_struct type used in the functions prototype list. For example, add_to_file wants this type declaration (are you sure type but not type*?).
Syntax error before token is a typical message from one-pass compilers when they detect undeclared struct/union (or class in C++) arg/var in declaration lists...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top