Home Up Fortran Software Engineering Personal page Data Structures Science of Computing Calculus I, II, III Basic IBM Assembler micro comp app pkgs Links Resume Class Hierarchy

Structures

A structure is a derived data type that represents a collection of related data items called components/members that are not necessarily of the same data type
A structure is a programmer defined data type and therefore must be declared in a program


Declaring a Structure

Example:
         struct StructureTypeName {
           StructureMemberDeclarationsList
         };
 

Requires these elements:
Keyword   struct
The structure type name
A list of variable names separated by commas
A concluding semicolon

Example of a struct

Let’s say our requirements outline the use of data that represents an employee. We might use a structure to manage the data in our program.
  struct Employee_Rec {
        char LastName[15];
                  char FirstName[10];
                  char SSN[12];
                  float HourlyRate;
                  int  HoursWorked;
  };

Assigning variable names

Creating a variable of the structure type can be done in two ways.
           struct Employee_Rec {
        char LastName[15];
                  char FirstName[10];
                  char SSN[12];
                  float HourlyRate;
                  int  HoursWorked;
  } Employee ;                                    At the end of the structure declaration, between the
                                                                   ending brace and the semi-colon.
                                                                                            OR
     struct Employee_Rec  Employee;       In a separate declaration statement.
 
 

More Complex Structures


Now let’s say we want to add the employee’s address to the record.  We again have many ways of accomplishing this.  One method is by using nested structures. First we declare a structure that will hold the address. We then use that structure within the structure for the employee.
Example of Nested Structures

struct Address_Rec {
  int House_Number;
         char StreetName[10];
         char City[10];
         char State[3];
         char ZipCode[6];
};
struct Employee_Rec {
        char LastName[15];
                  char FirstName[10];
                  struct Address_Rec Address;
                  char SSN[12];
                  float HourlyRate;
                  int  HoursWorked;
  } Employee ;

Operations on Structure Variables

  Copying the contents of a structure variable to another structure variable (both must be of the same structure type!) Passing structure variables or their members to functions.

Examples

struct Employee_Rec  Employee1, Employee2;

                       Employee2 = Employee1;

Passing to a function:

   CalcPay(Employee1);

    void CalcPay(struct Employee_Rec Employee) { …...

Functions

We could also create a function of the structure type.

 struct Employee_Rec Get_EmpInput(void);

   Employee1 = Get_EmpInput( );
 
 


Enumerated Data Types


C provides for a special user defined data type called enumeration. Defined with the keyword “enum” An enumeration is a set of integer constants which are represented by identifiers. Enumeration constants have their values set automatically when they are declared. Values start at 0 unless otherwise specified.

Examples of enum

 enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
 enum colors {RED, BLUE, GREEN, YELLOW, MAGENTA, ORANGE};
 enum cars {FORD, CHEVY, DODGE, SAAB, PORCHE, AUDI};

enum

enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

enum months {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

BOOLEAN Datatypes


In most high level programming languages a boolean data type is considered a fundamental data type (like integer, float). In C there is no Boolean data type. Using enum we can create the Boolean type for use within our programs.
                         enum  Boolean { FALSE, TRUE };

EXAMPLES

enum Boolean {FALSE, TRUE};
enum Mnths {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
char Month[13][10];
char Choice;
enum Boolean Done = FALSE;
enum Mnths M=JAN;
while (!Done) {
        printf(“Please enter month %d: “ M);
        scanf(“%s”, Month[M++]);
        printf(“Would you like to enter another? [Y/N] “);
        scanf(“%c”, &Choice);
        if (Choice == ‘N’) Done = TRUE;
  }
}
 
 

Pointer Variables


One of the most powerful features in C. Pointer’s are one of the most difficult capabilities of C to master. Pointers allow programmers to simulate “call by reference” and to implement dynamic data structures.

Pointer Operators



          int y = 5;
         int *yptr;
         yptr = &y;

Normal Variable

A variable name is the symbolic address of  a memory location which contains a value that is consistent with the declared type of that variable.

   int Number1
   int Number1 = 20;

Pointer Variables

Pointer Variables are variables whose values are memory addresses.

Declaring Pointer Variables

The  &  or address operator, is unary operator that returns the address of its operand.
The *or dereferencing or indirection operator, indicates that the variable being declared is a pointer.
C allows us to declare variables that contain as their values, memory addresses rather than integers, floating-point values, or characters.
These variables are called Pointer Variables.
All variables, including pointer variables, must be declared in C.
To declare a pointer variable we must
associate the variable with a data type
use the symbol * as its prefix


                                    int *Pointer_to_Number1;

Declares a variable as a pointer variable to a memory location that can store a value of type int.

Example

                                int Number1;
                                int *Ptr_to_Number;

                                Ptr_to_Number = NULL;

                                Number1 = 20
                                Ptr_to_Number = &Number1;

                                printf(“%d”, Number1);
                                printf(“%d”, *Ptr_to_Number);

Another Example

                                    #include <stdio.h>main( )
                                    {
                                       int *num;
                                       printf("Enter the number: ");
                                       scanf("%d", num);
                                       printf("The number was %d\n", *num);
                                    }

Example

                                #include <stdio.h>
                                main(){
                                   int y = 5;
                                   int *yptr;
                                   yptr = &y;
                                   printf("The address of y is %d\n", &y);
                                   printf("The value at y is %d\n", y);
                                   printf("The other address is %d”, yptr);
                                   printf(“and contains %d\n",  *yptr);
                                }

Parameter Passing by Pointer


We may design functions that can return multiple values by declaring their formal parameters as pointer variables. To pass the value of a variable by pointer between two functions we must: Declare the variable that is meant to return a value to the calling function as a pointer variable. Pass the address of the value to be passed from  the calling function. Declare the function prototype correctly.
Example

                            void Square_the_number( int*);
                            main( ){
                                int A_Number = 5;
                                Square_the_number(&A_Number);
                                printf(“%d”, A_Number);
                            }
                            void Square_the_number(int *Number){
                               *Number = *Number * *Number;
                            }

Example 2

                            void Get_3_numbers( int*, int*, int*);
                            main( ){
                                int Num1, Num2, Num3;
                                Get_3_numbers(&Num1,&Num2,&Num3);
                                printf(“%d %d %d”, Num1, Num2, Num3);
                            }
                            void Get_3_numbers(int *Num1, int *Num2, int *Num3){
                               printf(“Enter 3 Numbers: “);
                               scanf(“%d %d %d”, Num1, Num2, Num3);
                            }
 
 

Back Home Up Next