Passing structure data to a function?

This is a discussion on Passing structure data to a function? within the C Programming forums, part of the General Programming Boards category; Hey all... I was wondering what's the proper syntax for passing structure ints/chars, whatevers to a function... here's my function: ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    44

    Passing structure data to a function?

    Hey all... I was wondering what's the proper syntax for passing structure ints/chars, whatevers to a function... here's my function:

    void hi_val (int, int);

    It takes arguments, but no return type. I want to pass two ints to it... one is an int from function main and another is from a structure (struct employee[100].salary) ... do I even need the [100]? I can't seem to figure this out. TIA.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. You need the [100] if you are accessing the 101st element in an array of structures. If it is a single structure then no you don't.

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

  3. #3
    diddy02
    Guest
    Update - I need to pass an int and a int from a structure.... I have no idea how to do this. Please help.

  4. #4
    icarus
    Guest
    Update - I need to pass an int and a int from a structure.... I have no idea how to do this. Please help.
    if you have
    Code:
    void funct(int a, int b) {...}
    then you can call it like so
    Code:
    funct(struct1.num, struct2.num);
    I think

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    that's how you would do it, assuming that the structs had 'num' as an int
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    And then how do you define the function? Sorry, I'm just really confused... what you said before just results in more errors.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by diddy02
    And then how do you define the function? Sorry, I'm just really confused... what you said before just results in more errors.
    Well if the function takes 2 ints then it doesn't care where they come from. Someone above posted the correct way to pass two integers from a structure to a function. Now, if you wanted to pass the entire structure to the function that is a different story. Try this if that is the case.

    Code:
    void myfunc( struct struct_name_here *pStruct )
    {
       int number;
       number = pStruct->iNum;
    }
    Something like that. Not sure exactly what you are doing without seeing more code.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Haven't done pointers yet.... So I'm supposed to do the program w/out them. TIA.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    				  
    			    struct employees
    			
    		   		  {
    				   		   int salary;
    						   char firstname[40];
    						   char lastname[40];
    						   char departmentname[40];	   
    				  } employee[50];
    				  
    				  
    				/* struct departments
    		   
    		   		  {
    				   			int department_number;
    							char department_name;
    				  } department; */
    				  
    				  
    void main()
    
    	 {
    	 
    	  	   int x, y, z;
    		   int nmbrofdeps;
    		   int nmbrofeps;
    		   int tmp, strtmp;
    		   int op1, op2; 
    		   
    		   char dprtmnt_in[40];
    		   
    		   void high_val(struct employees emp_data);
    		   
    		   //employee[0].salary=0;
    		   //employee[0].firstname='df';
    		   //employee[0].lastname='df';
    		   //employee[0].departmentname='df';
    		   
    		   						  printf("Please enter the following data.");
    								  printf("\nHow many employees' data would you like to enter?");
    								  scanf("%d", &nmbrofeps);
    								  
    								  for(x=1; x<=nmbrofeps; x++)
    								  		   {
    										   		printf("\nEnter Employee's First Name : ");
    												scanf("%s", &employee[x].firstname);
    												printf("\nEnter Employee's Last Name : ");
    												scanf("%s", &employee[x].lastname);
    												printf("\nEnter Employee's Salary : ");
    												scanf("%d", &employee[x].salary);
    												printf("\nEnter Employee's Department Name : ");
    												scanf("%s", &employee[x].departmentname);
    											}
    
    menustart:									 			clrscr();
    																						printf("\n\n\t\t########Options########");
    																						printf("\n\t\t1 - See Employee listing under a Department.");
    																						printf("\n\t\t2 - Find Employee with the highest salary in a Department.");
    																						printf("\n\t\t3 - Find Employee with the highest salary from all Departments.");
    																						printf("\n\t\t4 - Exit Program.");
    optionreset:																															
    																						printf("\n\n\n\nEnter your choice here: ");
    																						scanf("%d", &op1);
    																						
    																						switch(op1)
    																								   {
    																								   		 case 1: break;
    																										 	  	 	
    																										 case 2: goto hds;
    																										 	  	 	
    																										 case 3: goto hs;
    																										 	  	 	 
    																										 case 4: goto end;
    																										 	  	
    																													  
    																										default: printf("Only enter a number from the options please.");
    																												    goto menustart;
    																									}
    
    											
    				clrscr();
    
    											printf("\n\nType a department name to list all of it's employees.");
    											printf("\nHere are the department names you have inputted.");
    											
    														 for(y=1; y<=nmbrofeps; y++)
    														 		  {
    																				printf("\n%s", employee[y].departmentname);
    																  }
    														printf("\n\n\n");		  
    														scanf("%s", &dprtmnt_in);
    														
    																				 
    																				 for(z=1; z<=nmbrofeps; z++)
    																				 		  {
    																						   				strtmp=strcmp(employee[z].departmentname, dprtmnt_in);
    																						   				if(strtmp==0)
    																													 {
    																													  		printf("\n\n");
    																															printf("\nEmployee's First Name : %s", employee[z].firstname);
    																															printf("\nEmployee's Last Name : %s", employee[z].lastname);
    																															printf("\nEmployee's Salary : %d", employee[z].salary);
    																															printf("\nEmployee's Department : %s", employee[z].departmentname);
    																															printf("\n\n");
    																															getch();
    																															goto menustart;
    																												 	 }
    																										if(strtmp!=0)
    																													 {
    																													  		printf("\nNo such Department exists.");
    																															goto menustart;
    																													 }			 
    																													 
    																													 
    																						 }
    																															
    																															
    
    
    
    hds:						high_val(employee);
    
    hs:
    																								
    																								
    																								
    																													
    																															
    																															
    																					
    end:																			
    																					
    																					getch();
    																					
    																					
    						}
    						
    						
    		void high_val(struct employees emp_data)
    		
    		// high val function //

    Yeah, a LOT of tabs... but I use this program that inputs them for no real reason... sorry for that. TIa.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, a few tips for void:

    - Sort the tabs out! I know you said you used a prog that puts them there, but even so, sort it out!

    >void main
    Nope, main returns an int. Use
    >int main(void)
    and return 0; at the end of the function.

    >scanf("%d", &nmbrofeps);
    For safety sake, check the return code of scanf() to ensure it did what you wanted.

    >for (x = 1; x <= nmbrofeps; x++)
    Doing this means element 0 of the array will never get populated. Is this what you meant to do?

    >goto
    Try not to use goto. There are better ways to control the flow of your program. goto is a pet hate for a lot of people, and is only used in certain circumstances. It certainly isn't needed in the example code you have posted.

    >strtmp
    This is a reserved name. You shouldn't create variable starting str....

    And finally, what is your question again?? Which part are you having trouble with?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM
  5. Passing a structure to a function?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2001, 04:28 AM

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