Thread: structure array

  1. #1

    Question structure array

    I'm trying to get the info and put it into a structure and the structure is an array. I don't have any experience in this. Any help would be greatly appreciated!! The error I get is: error C2664: 'display' : cannot convert parameter 1 from 'struct employee' to 'struct employee []'. It is pointing to case 2 in the main function.
    struct employee {
    			char name[30];
    			char ssn[12];
    			float rate;
    			float hours;
    			float gross;
    			float tax;
    			float net;
    int menu();
    struct employee info(void);
    void display(struct employee emp[10]);
    int main(void)
    	int sel;
    	struct employee emp[10];
    	while ((sel = menu()) != -1)
    		switch (sel)
    			case 0: return 0; break;
    			case 1: emp[10] = info(); break;
    			case 2: display(emp[10]); break;
    			default: puts("\a\nInvalid Selection\n");break;
    	return 0;
    struct employee info(void)
    	int num;
    	struct employee emp[10];
    	for(num = 0; (num < 10); num++){
    	printf("Enter employee's name (no spaces): ");
    	scanf("%s", &emp[num].name);
    	printf("Enter employee's SSN (999-99-9999): ");
    	scanf("%s", &emp[num].ssn);
    	printf("Enter employee's payrate: ");
    	scanf("%f", &emp[num].rate);
    	printf("Enter employee's hours worked: ");
    	scanf("%f", &emp[num].hours);
    	emp[num].gross = emp[num].hours * emp[num].rate;
    	emp[num].tax = emp[num].gross * 0.21;
    	emp[num].net = emp[num].gross * 0.79;
    	return emp[num];
    int menu()
    	int sel;
    	puts("\nMenu\nPress 1 to input data\nPress 2 to display data\nPress 0 to Quit");
    	scanf("%d", &sel);
    	return sel;
    void display(struct employee emp[10])
    	int choice;
    	puts("Name            SSN           Gross Pay               Taxes          Net");
    	for ( choice = 0 ; (choice < 10) ; choice++ ) {
    	printf( "%s        %s            %f          %f        %f", emp[choice].name, emp[choice].ssn, emp[choice].gross, emp[choice].tax, emp[choice].net);

  2. #2
    Join Date
    Aug 2001
    Groningen (NL)

    Here you pass the 10th struct of emp to the function display. But the function display expects an array of structs instead of a struct.
    Also note that there is no struct with index 10, indices start with 0. So the last index is 9.

    The for-loop in function info is also wrong, I guess you want to fill 10 structs and send the array back. In your function, the for-loop is immediately exited because of the return. You could send back the address of the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM