Structures, passing array of structures to function

This is a discussion on Structures, passing array of structures to function within the C Programming forums, part of the General Programming Boards category; This involves structures. Basically I created an array of structures and I am supposed to pass it onto a function ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    63

    Structures, passing array of structures to function

    This involves structures. Basically I created an array of structures and I am supposed to pass it onto a function that was given. This is where my problem is (marked /*HERE*/). I think I am passing it onto the function incorrectly. This is the error I get:
    warning C4133: 'function' : incompatible types - from 'struct Player [8]' to 'struct Player *'

    Im not exactly sure what that means. something to do with passing on a pointer? Could someone help me out? What do I need to pass to the function?

    I think I assured myself that I created the array properly by using printf statements.

    NOTE: the two functions printBracket and getIndex were given, so they cannot be changed.

    Let me know if I need to explain better.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*FUNCTION PROTOTYPES*/
    
    void printBracket( struct Player x []);
    int getIndex( char c );
    
    int main(){
    
    	//initialize variables
    	struct Player{
    		char major;
    		char name[10];
    		int seed;
    	};
    
    	struct Player array[8];
    
            int count=1;
    	char person[10];
    	char subject;
    	int junkChar;
    
            //Print statement
    	printf("Welcome to MARCH MADNESS!\n\nPlease enter the 8 player names, "
    	   "followed by their major code and seed.\n\n(For major enter a single "
    	   "character from the major code key:\n\n\t"
           "'e' for ECE, 'h' for ChE, 'm' for MIE\n\t"
           "'c' for CEE, 'b' for BME, 'u' for Und )\n");
    
    	while(count<=8){             //while loop to get users inputs
    		printf("Enter the number %d seed's name (10 chars max):",count);  //prompt for name
    		scanf("%s",&person);
    
    		printf("Enter %s's major code: ",person);//prompt for major 
    		scanf("%c",&junkChar);
    		scanf("%c",&subject);
    
    		strcpy(array[count-1].name,("%s",person));
    		array[count-1].major=subject;
    		array[count-1].seed=count;
    
    		count++;  //add 1 to counter
    
    	} //end while
    
    	printBracket(array); /*HERE*/
    
            return 0; //indicate program ended successfully
    
    } //end function main
    
    
    /**FUNCTIONS**/
    
    //FUNCTION: printBracket
    //ARGUMENTS: ( struct Player [] ): Array of team structures sorted by seed
    //RETURN: void
    //DESCRIPTION: prints out bracket image of sorted array of players
    void printBracket( struct Player x [] )
    {
    	char * maj[6]= {"ECE", "ChE", "MIE", "CEE", "BME", "Und"};  //array used for indexing major
    
    	//PRINTING THE BRACKET
    	printf("\n\n");
    	printf("__%-10s_(%s)_(%d)_\n", x[0].name, maj[getIndex(x[0].major)], x[0].seed );
    	printf("                       |_________\n");
    	printf("__%-10s_(%s)_(%d)_|         |\n", x[7].name, maj[getIndex(x[7].major)], x[7].seed );
    	printf("                                 |_________\n");
    	printf("__%-10s_(%s)_(%d)_          |         |\n", x[3].name, maj[getIndex(x[3].major)], x[3].seed );
    	printf("                       |_________|         |\n");
    	printf("__%-10s_(%s)_(%d)_|                   |\n", x[4].name, maj[getIndex(x[4].major)], x[4].seed );
    	printf("                                           |_________\n");
    	printf("__%-10s_(%s)_(%d)_                    |\n", x[1].name, maj[getIndex(x[1].major)], x[1].seed );
    	printf("                       |_________          |\n");
    	printf("__%-10s_(%s)_(%d)_|         |         |\n", x[6].name, maj[getIndex(x[6].major)], x[6].seed );
    	printf("                                 |_________|\n");
    	printf("__%-10s_(%s)_(%d)_          |\n", x[2].name, maj[getIndex(x[2].major)], x[2].seed );
    	printf("                       |_________|\n");
    	printf("__%-10s_(%s)_(%d)_|\n", x[5].name, maj[getIndex(x[5].major)], x[5].seed );
    
    }
    /////////////////END OF printBracket FUNCTION\\\\\\\\\\\\\\\\\\\\\\\\\\\//FUNCTION: getIndex
    //ARGUMENTS: ( char ): character to index
    //RETURN: int
    //DESCRIPTION: returns the appropriate index of the major character in the string array of printBracket
    int getIndex( char c )
    {
    	switch( c )
    	{
    		case 'e':
    		case 'E':
    			return 0;
    			break;
    		case 'h':
    		case 'H':
    			return 1;
    			break;
    		case 'm':
    		case 'M':
    			return 2;
    			break;
    		case 'c':
    		case 'C':
    			return 3;
    			break;
    		case 'b':
    		case 'B':
    			return 4;
    			break;
    		case 'u':
    		case 'U':
    			return 5;
    			break;
    		default:
    			return 5;
    	}
    }
    /////////////////END OF getIndex FUNCTION\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop defining your structures inside functions.


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

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by quzah
    Stop defining your structures inside functions.


    Quzah.
    Yeh, figured that out. Also needed a pause at end of main function.

    Its all good now.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    One more question. Why wont this work? Im using the OR's correctly right?

    Code:
    if(major!='e'||'h'||'m'||'c'||'b'||'u'){
    			printf("ERROR! Invalid Error Code Entered\n");
    			count--;
    		}
    But it works if I split it up into an if statement for each character.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    if(major!='e'||'h'||'m'||'c'||'b'||'u'){
    			printf("ERROR! Invalid Error Code Entered\n");
    			count--;
    		}
    You are comparing major to 1.
    Code:
    if(major!='e'||major!='h'||major!='m'||major!='c'||major!='b'||major!='u'){
    			printf("ERROR! Invalid Error Code Entered\n");
    			count--;
    		}
    Keep the switch and make this the default.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by Dave_Sinkula
    Code:
    if(major!='e'||'h'||'m'||'c'||'b'||'u'){
    			printf("ERROR! Invalid Error Code Entered\n");
    			count--;
    		}
    You are comparing major to 1.
    Code:
    if(major!='e'||major!='h'||major!='m'||major!='c'||major!='b'||major!='u'){
    			printf("ERROR! Invalid Error Code Entered\n");
    			count--;
    		}
    Keep the switch and make this the default.
    Thats not working either for some reason.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    D'oh! I knew I should have spent more time before posting. The || need to be &&.

    Or again, go with the switch.
    Code:
       switch ( major )
       {
       case 'e': case 'h': case 'm': case 'c': case 'b': case 'u':
          /* do stuff */
          break;
    
       default:
          printf("ERROR! Invalid Error Code Entered %c\n", major);
          return 0;
       }
    Last edited by Dave_Sinkula; 04-05-2006 at 04:20 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by Dave_Sinkula
    D'oh! I knew I should have spent more time before posting. The || need to be &&.

    Or again, go with the switch.
    Code:
       switch ( major )
       {
       case 'e': case 'h': case 'm': case 'c': case 'b': case 'u':
          /* do stuff */
          break;
    
       default:
          printf("ERROR! Invalid Error Code Entered %c\n", major);
          return 0;
       }
    Oh, I should have figured that out. Thanks.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Code:
    scanf("%s",&person);
    incorrect i belive it should be
    Code:
    scanf("%s",person);
    Its an array. so u dont need the &

    anyways its probably not the major concern

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by tama00
    Code:
    scanf("%s",&person);
    incorrect i belive it should be
    Code:
    scanf("%s",person);
    Its an array. so u dont need the &

    anyways its probably not the major concern
    Hmm.. It seems to work either way.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just because it "seems to work", doesn't make it right. You'd better learn this fast if you expect to be anywhere near competent as a programmer.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-31-2009, 03:44 PM
  2. passing an array to function
    By waysgoose in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 04:59 AM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM

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