Home ] Up ] ComputingConcepts ] Definitions ] Characters & Strings ] C_Formatted IO ] C_Structs_Enums_Files ] FunctionsAndMenus ] Arrays and Cmd Line Args ] [ Arrays & Structures ] Structs, Enums, Ptr Variables ] Ptrs. to External Files ] Miscellaneous ] Random Files ]

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.

 

Example 1

#include <stdio.h>
 void Get_Name(char[ ]);

 main(){
    char Name[15];
    Get_Name(Name);
    printf("%s\n", Name);
}

void Get_Name(char Name[ ]) {
    printf("Please input name: ");
    scanf("%s", Name);
}

Example 2

#include <stdio.h>
void Get_Names(char[ ][15]);
void Get_Nums(int [ ]);
main(){
    int k;
    int Nums[5];
   char MyArray[5][15];

   Get_Names(MyArray);
   Get_Nums(Nums);
   for (k=0;k<5;k++){
        printf("%s\n", MyArray[k]);
        printf("%d\n", Nums[k]);
   }
}

void Get_Nums(int Nums[5])
    int k;
    for (k=0;k<5;k++){
          printf("Input number: ");
          scanf("%d", &Nums[k]);
    }
}

void Get_Names(char TheArray[ ][15]) {
    int j;
    for(j=0;j<5;j++){
          printf("Please input name %d: ", j+1);
          scanf("%s", TheArray[ j]);
    }
}

Example 3

#include <stdio.h>

void Get_Grades(int [ ][5];

main(){
  int Grades[5][5];
  int j, k;
  Get_Grades(Grades);
  for(j=0;j<5;j++){
     for(k=0;k<5;k++)
          printf(“%d “, Grades[j][k]);
     printf(“\n”);
 }
}
Get_Grades(int G[ ] [5]) {
    int j,k;
    for (j=0;j<5;j++) {
        for(k=0;k<5;k++) {
            printf ("Enter grade:");
            scanf ("%d", &G[j],[k]);
            }
        printf("\n");
        }
    }

Structures


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.
 

Creating an Array of Structures

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];
 
 

Back Home Up Next