struct passing

This is a discussion on struct passing within the C Programming forums, part of the General Programming Boards category; Im using an ansi c gcc comiler and im trying to pass a structure variable from a seperate file into ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    struct passing

    Im using an ansi c gcc comiler and im trying to pass a structure variable from a seperate file into the main.c file and when i do i get the errors unidefied symbol 'ts" warning argument 1 is incompatiable with prototype what am i doing wrong?




    Code:
    
    
    int commandLineArguments(int, char**);
    int MainMenu(int*);
    
    int main(int argc, char** argv)
    {
       int x = 10;
    
       TennisStoreType ts;
       commandLineArguments(argc, argv);
       MainMenu(&x);
    
       return EXIT_SUCCESS;
    }
    
    int commandLineArguments(int argc, char **argv) 
    {   
        /* makes a local copy of argc and **argv */
    
     
       FILE *customer, *stock, *logfile; 
       
        /* declares a pointer to each file*/
    
        /* test if 4 arguments exist including the exucutable*/
    
       if(argc !=4) 
       {
         printf("please enter 3 cmd arguments!\n");
         return 1;  
         /* stop pocessing*/
       }
    
        /* attempt to open and read arg 1*/
    
       customer = fopen(argv[1], "r");
    
        /* check if custome.csv exists*/
    
       if(customer ==NULL)
       {
         printf("cannot open %s for reading\n", argv[1]);
         return 1;  
         /* can't go on*/
       }
    
         /* try to open stock.csv and read it*/
    
       stock = fopen(argv[2], "r");
      
         /* if stock.csv does not exist error*/
       
       if(stock == NULL)
       {
         printf("cannot open %s for reading\n",argv[2]);
         return 1;  
    
         /* stop processing*/
       }
    
         /* open logfile for reading */  
     
       logfile = fopen(argv[3], "r");
    
         /* check if logfile exists*/
    
       if(logfile == NULL)
       {
         printf("cannot open %s for reading\n", argv[3]);
         return 1;  
     
         /* stop processing*/
       }
       
         /* close all files */
    
       fclose(customer);
       fclose(stock);
       fclose(logfile);
    
       return 0;
    
        
    }
    
    int MainMenu(int *a)
    {
    
      int choice = 0;
    
      /* declare variables*/
      
      /* keep doing this until its not equal to 9*/
     
      do
       {
    
         printf("\n\t*****Main Menu:\t*****\n");
         printf("1) Add Customer\n");
         printf("2) Add Stock\n");
         printf("3) Delete Record\n");
         printf("4) Display Customer\n");
         printf("5) Display stock\n");
         printf("6) Make Sale\n");
         printf("7) Sales Log\n");
         printf("8) Save and Exit\n");
         printf("9) Abort\n");
       
         printf("Select Your option (1-9):\n");
    
         fscanf(stdin, "%d",&choice);
         
         /* User Input */
       
         readRestOfLine(); 
         
         /* flushes the stdin buffer for better input*/
    
         /* switch statement to select function*/
    
         switch(choice)
         {
           case 1:
           printf("Add stock");
    
           addCustomer(&ts);
          /* i want to pass it into addCustomer somehow*/
    
           break;
    
         }
    
      }
      while(choice !=9);
    
      /* end of do while */  
    
      /* switch case */
    
      /*  do while
    
      /* while !=10 */  
    
      return 0;
    }
    
    
    /* then in the second file which contains all the menu option functions such as addCustomer*/
    
    void addCustomer(TennisStoreType* ts)
    {
    
         printf("It better call this function!");
    }

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    ts is declared in main, thus it is only visible in main. It is not visible to the compiler in your other functions. You need to pass a pointer to the ts struct to MainMenu first.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    could you give me an example sorry for the bother im having allot of trouble passing variables

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct foo
    {
        ...foo stuff...
    };
    
    void fun( struct foo *f )
    {
        ...knows about foo because the structure was defined before the function...
    }
    
    int main( void )
    {
        ...knows about the structure and the function for the same reason...
    }
    It has to do with the scope of whatever object you're dealing with. The structure is made known to everything below it. The same thing goes with the function.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. passing struct
    By Doxygen in forum C Programming
    Replies: 2
    Last Post: 10-26-2006, 03:05 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Passing a struct
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 03-14-2005, 10:02 AM
  5. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM

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