C PROGRAMMING
PASSING
ARRAYS TO FUNCTIONS
AND
INTRODUCTION
TO STRUCTURES
Passing Arrays to Functions
![]() | When you are passing an array variable to a function, all that needs to be passed is the name of the array (without brackets). |
int MyArray[15]; Input_Array(MyArray); |
Returned
Values
![]() | When you pass an array to a function, C uses a simulated “call by reference”. |
![]() | Which means that the original array values are modified in the “calling” program. |
![]() | Arrays do not have to be “returned.” |
![]() | The name of the array is actually the address of the first element of the array. |
#include <stdio.h>
main(){
void Get_Name(char
Name[ ]) {
|
Example 2
#include <stdio.h>
Get_Names(MyArray);
void Get_Nums(int
Nums[5])
void Get_Names(char
TheArray[ ][15]) {
|
Example 3
#include <stdio.h> void Get_Grades(int [ ][5]; main(){
|
A Structure is a group item that can hold two or more data types. The contents of a structure are called members. A structure is a record and the members of the structure are fields. A structure must be declared before it can be used by the program. The keyword struct is used to define a structure.
Creating a Structure
Format:
struct tag { type member 1; type member 2; . . . . . . type member n; }; |
Structure Example
Example:
struct StuRec { char fname[10]; char lname[15]; int StuIDNum; }; |
Defining a Structure Variable
Once you have declared a structure, you can use it to define one or more variables of that structure type.
Example:
struct StuRec Student;
Assigning Data to a struct Variable
Data can be assigned to a structure variable at the time it is declared or during the execution of the program. Format:
struc StuRec Student = (“John”, “Smith”, 125);
Interactive loading of data
printf (“Enter
Student first Name: “);
scanf(“%s”, Student.fname); printf(“\nEnter Student last Name: “); scanf(“%s”, Student.lname); printf(“\nEnter Student ID Number: “); scanf(“%i”, &Student.StudIDNum); Notice the use of the “dot” operator! |
Arrays of Structures
![]() | To process multiple data types that have a relationship or association we have had to use parallel arrays. |
![]() | It is possible to use an array of structures to replace the parallel array requirements. |
The first step is to define the structure that represents the data you wish to model.
struct EmployeeRec {
char fname[10];
char lname[15];
int hours[5];
float PayRate;
};
Create the Array
The next step is to create an array of the structure type.
Struct EmployeeRec
EmpList[100];