Thread: Help with error in function with structure

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    Help with error in function with structure

    I have a program which essentially is a spreadsheet, which I run as a double array (rows and columns) of a structure cell. When write my function prototype it gives me errors on compile, not sure why....

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void print_cell(cell sheet[][]);  <--------- COMPILE ERROR: error: expected ‘)’ before ‘sheet’
    
    typedef struct cell
    {
    	int type;   // 0-none 1-number 2-text 3-forumula
    	char text[32];
    	int form;	//1-avg 2-sum 3-min 4-max
    	int num;
    }cell;
    
    int main()
    {
    // 9 by 9 spreadsheet
    cell sheet[9][9];

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct cell
    {
    	int type;   // 0-none 1-number 2-text 3-forumula
    	char text[32];
    	int form;	//1-avg 2-sum 3-min 4-max
    	int num;
    }cell;
    
    /*
     *  Flop this two sequences of code
     */
    
    void print_cell(cell sheet[][]);  <--------- COMPILE ERROR: error: expected ‘)’ before ‘sheet’
    
    int main()
    {
    // 9 by 9 spreadsheet
    cell sheet[9][9];

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to tell the compiler that you have a struct cell called cell before you use cell as a typename. In other words, move the typedef-struct part to BEFORE print_cell.

    You also can't pass a two-dimensional array to a function wihtout giving at least the first dimension, e.g. if you have 100 rows (or columns, depending on which you have first), you could do this:
    Code:
    void print_cell(cell sheet[100][]);
    --
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    thanks all, that got rid of that compile error, and graced me with new ones....

    Error on func prototype: " array type has incomplete element type"
    when calling function: "type of formal parameter 1 is incomplete"
    in function itself: "array type has incomplete element type"

    is this because i am using a typedef for my struct? I thought that by using a typedef it would make all the functions simpler as I could use "cell" as my type, just as int or double or char

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by matsp View Post
    You need to tell the compiler that you have a struct cell called cell before you use cell as a typename. In other words, move the typedef-struct part to BEFORE print_cell.

    You also can't pass a two-dimensional array to a function wihtout giving at least the first dimension, e.g. if you have 100 rows (or columns, depending on which you have first), you could do this:
    Code:
    void print_cell(cell sheet[100][]);
    --
    Mats
    Did you catch that part of Mats' post?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Yes, I've actually tried this with the prototype and function being...
    void print_cell(cell sheet[][])
    void print_cell(cell sheet[9][])
    void print_cell(cell sheet[9]9])

    all these possibilities give me the exact same compile errors.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    and I have placed the typedef above the prototype as well

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you post the exact code you have right now? The only error I get is that print_cell is not undefined - which is no surprise, since there is no print_cell function defined in the code you've posted.

    --
    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.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    woooooooo... I got it to compile, I basically just erased everything to do with the function and typed it over, and I got it right, must have been a small typo somewhere. Thank you all very much for the help!!

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You are quite welcome, good sir. It was a pleasure doing business with you. Your bill should arrive via FedEx Air tomorrow afternoon.

  11. #11
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by master5001 View Post
    You are quite welcome, good sir. It was a pleasure doing business with you. Your bill should arrive via FedEx Air tomorrow afternoon.
    What's your rate these days? 500 an hour? Too bad for those who fail to read the ToS when signing up.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    i'm actually planning on moving to the far east to become a monk and have thus sold all my belongings.... so i won't be able to get that bill, but i'll pray for you, hows that?

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That was pretty awesome. Welcome to the boards, friend.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    haha thanks.... c amateur, sarcasm master

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have learned you are halfway to the point of becoming one of us regular users. Though I have to admit, some users took years to get to being as sarcastic as they are now.

    [edit]I think Todd may have a quote of the month contender sweeping in at the 9th hour[/edit]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM