Calling a function: syntax error before ']' token

This is a discussion on Calling a function: syntax error before ']' token within the C Programming forums, part of the General Programming Boards category; Hey guys, I've got a problem that I'm hoping someone can shed some light on... Here's the code: Code: //prototype// ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    43

    Calling a function: syntax error before ']' token

    Hey guys, I've got a problem that I'm hoping someone can shed some light on...

    Here's the code:
    Code:
    //prototype//
    
    void load_array_from_file(FILE* fp, int quote[], char roomletter[],
    int length[], int width[], char paint[], char ceiling[],
    double cost[], double setup_cost[],
    int* plines_in_file);
    
    //main//
    
    main(){
           int* poption;
           FILE* fp;
           int quote, length, width;
           int* plines_in_file;
           char roomletter, paint, ceiling;
           double cost, setup_cost;
           load_array_from_file(*fp, quote[], roomletter[], length[], width[], paint[], ceiling[], cost[], setup_cost[], *plines_in_file);
           }
    
    //function//
    
    void load_array_from_file(FILE* fp, int quote[], char roomletter[],
    int length[], int width[], char paint[], char ceiling[],
    double cost[], double setup_cost[],
    int* plines_in_file){
         fp = fopen("C:/Users/ASDF/Desktop/A/quotes.txt","r");
         if(fp == NULL){
               printf("Error\n");
               }
         else{
         fscanf(fp, "%d,%[^:],%d,%d,%[^:],%[^:],%lf,%lf", quote, roomletter, length, width, paint, ceiling, cost, setup_cost);
         printf("%d,%[^:],%d,%d,%[^:],%[^:],%lf,%lf",quote, roomletter, length, width, paint, ceiling, cost, setup_cost);
               }
         }
    So there error code I get is:
    In function 'main':
    syntax error before ']' token

    That is on line 33, which is this line:
    load_array_from_file(*fp, quote[], roomletter[], length[], width[], paint[], ceiling[], cost[], setup_cost[], *plines_in_file);



    Any idea what I'm doing wrong? Basically I'm trying to read certain values from a file and output it, but I'm stuck here.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    If I change that line to:

    void load_array_from_file(FILE* fp, int quote[], char roomletter[],
    int length[], int width[], char paint[], char ceiling[],
    double cost[], double setup_cost[],
    int* plines_in_file);

    It compiles but nothing happens when I run the program.

  3. #3
    Registered User mdj441's Avatar
    Join Date
    Aug 2011
    Location
    NYS, USA
    Posts
    18
    Your change works because you're just duplicating your prototype from line 3 without calling the function.

    The fact that you'd write your code like that implies you haven't really dealt with functions/arrays/pointers before.
    I'd work on how to define and call a function first before I'd play with pointers! It would probably lessen the confusion a bit.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    I have dealt with functions, arrays, and pointers before, but more at a basic level.

    The prototypes were issued by the professor.

    What I've done now is taken the printf("") out of the function and placed it in main so that printf uses the values called from the function but it's not working right.

    Code:
    main(){
           int* poption;
           FILE* fp;
           int quote, length, width;
           int* plines_in_file;
           char roomletter, paint, ceiling;
           double cost, setup_cost;
           
           void load_array_from_file(FILE* fp, int quote[], char roomletter[],
           int length[], int width[], char paint[], char ceiling[],
           double cost[], double setup_cost[],
           int* plines_in_file);
           
           printf("%d,%c,%d,%d,%c,%c,%lf,%lf", quote, roomletter, length, width, paint, ceiling, cost, setup_cost);
           }
    
    //function://
    void load_array_from_file(FILE* fp, int quote[], char roomletter[],
    int length[], int width[], char paint[], char ceiling[],
    double cost[], double setup_cost[],
    int* plines_in_file){
         fp = fopen("C:/Users/Vartan/Desktop/IPC/quotes.txt","r");
         if(fp == NULL){
               printf("Error\n");
               }
         else{
         fscanf(fp, "%d,%c,%d,%d,%c,%c,%lf,%lf", quote, roomletter, length, width, paint, ceiling, cost, setup_cost);
               }
         }
    Output is:

    2, , 7,28,", ,0.000000,0.000000

    Any idea on how to fix it?

    edit:

    I should mention, the file contains the following:
    531,A,10,10,b,n,96.00,100.00
    531,B,15,15,b,n,144.00,0.00
    531,C,20,20,b,n,192.00,0.00
    533,A,11,11,r,n,123.20,100.00
    533,B,22,22,r,n,246.40,0.00
    534,A,10,16,p,n,166.40,100.00
    534,B,20,32,p,n,332.80,0.00
    535,A,10,10,b,y,96.00,100.00
    535,B,15,15,b,y,144.00,0.00
    535,C,20,20,b,y,192.00,0.00
    537,A,11,11,r,y,123.20,100.00
    537,B,22,22,r,y,246.40,0.00
    538,A,10,16,p,y,166.40,100.00
    538,B,20,32,p,y,332.80,0.00
    Last edited by MC++; 12-05-2011 at 07:24 PM. Reason: update

  5. #5
    Registered User mdj441's Avatar
    Join Date
    Aug 2011
    Location
    NYS, USA
    Posts
    18
    Alright, I understand. That's probably a step in the right direction.

    You should be passing the address of your variables to load_array_from_file() to use in fscanf() because fscanf() expects pointers in the first place.

    Although the prototype for load_array_from_file() and your file format sort of imply that you shouldn't be dealing with individual variables and their addresses at all but instead arrays, and that you'd have a loop in load_array_from_file() to read each line. I'm a bit confused.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Ugh, it seems the post I just made disappeared.

    edit:

    I'm sorry, I should have mentioned that arrays WILL be a part of the program, but right now I'm just building the basic mechanics and am trying to make sure it correctly outputs what's stored on the file (eg 531,A,10,10,b,n,96.00,100.00). Right now it juts seems to be spitting out random numbers.

    Could you please correct the code that seems to be wrong? Should I be assigning pointers to all my variables in the called function? Thanks
    Last edited by MC++; 12-05-2011 at 07:52 PM. Reason: update2

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    If anyone could tell me why my output from the fscanf/printf is:
    2, ,8,32,", ,0.00,0.00

    I would be eternally grateful. Thank you. I'll check this thread tomorrow morning.

    edit:
    Interesting. For fun I decided to modify the file path for the fopen portion, and it seems that no matter what I put there the output remains the same. The path and filename are all correct, but I'm assuming I'm doing something wrong with fopen. Can anyone tell me what I'm doing wrong? Wrong syntax perhaps?

    Thanks
    Last edited by MC++; 12-05-2011 at 08:35 PM. Reason: updated

  8. #8
    Registered User mdj441's Avatar
    Join Date
    Aug 2011
    Location
    NYS, USA
    Posts
    18
    Your function correctly accepts pointers as that's what fscanf() needs, so you need to pass it pointers: either
    Code:
    load_array_from_file(&var_from_main, ...) // NOT var_from_main[]
    or
    Code:
    load_array_from_file(array_from_main, ...) // same idea
    Your output is bad because, if you haven't changed your code, you still aren't even calling your function! In the place of a function call, you've only shown 1) a syntax error and 2) a duplicate prototype.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Is there any chance you could rewrite the parts that are messed up? It's late and I've had a lot of issues to deal with lately. I'd like to see exactly what needs to be changed so I could better analyze my mistakes.

    Thanks for your help so far, mdj441

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax Error Before ';' Token
    By Mike4 in forum C Programming
    Replies: 4
    Last Post: 12-01-2010, 10:23 AM
  2. Syntax Error Before ';' Token
    By subhadeepgayen in forum C Programming
    Replies: 10
    Last Post: 07-11-2010, 12:29 PM
  3. syntax error before '?' token
    By nullifyed in forum C Programming
    Replies: 9
    Last Post: 05-26-2010, 07:02 AM
  4. syntax error before '=' token
    By vsovereign in forum C Programming
    Replies: 3
    Last Post: 02-28-2010, 12:53 PM
  5. Syntax Error before ; token
    By Bassglider in forum C Programming
    Replies: 13
    Last Post: 12-11-2007, 11:05 PM

Tags for this Thread


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