![]() | 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 |
![]() | Requires these elements: |
![]() | Keyword struct |
![]() | The structure type name |
![]() | A list of variable names separated by commas |
![]() |
A concluding semicolon |
Assigning
variable names
More Complex 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
struct Employee_Rec Employee1, Employee2;
Employee2 = Employee1;
Passing to a function:
CalcPay(Employee1);
void CalcPay(struct Employee_Rec Employee) { …...
struct Employee_Rec Get_EmpInput(void);
Employee1
= Get_EmpInput( );
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
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;
}
}
![]() | 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 |
int *Pointer_to_Number1;
Declares a variable as a pointer variable to a memory location that can store a value of type int.
Example
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