Structures
Structures
1. Objective ..............................................................2
2. Structure Definition .............................................2
3. Struct Syntax........................................................3
4. Manipulating Structure Types..............................6
5. Arrays of Structures .............................................8
6. Hierarchical structure .........................................12
7. Function with a Structure Input Parameter ........15
8. Questions/Practice .............................................16
typedef struct {
char name[20];
int age;
} person;
struct telephone {
char name[30];
int number;
};
sturct_name var1;
o Examples:
person p1;
person p2;
Structure assignment:
o The copy of an entire structure can be easily done
by the assignment operator.
o Each component in one structure is copied into
the corresponding component in the other
structure.
eventDate ev1;
person class[5];
strcpy(class[0].name, “John”);
class[0].age = 19;
strcpy(class[1].name, “Sara”);
class[1].age = 18;
.name .age
class[4].name
Full Program:
//gcc 5.4.0
#include <stdio.h>
int main(void)
{
typedef struct {
int account_number;
char account_type[20];
char account_holder_name[40];
double balance;
} bank_customer;
bank_customers[0].account_number = 1001;
strcpy(bank_customers[0].account_type, "Checking");
strcpy(bank_customers[0].account_holder_name, "John
Paul");
bank_customers[0].balance = 2100.50;
for(int i=0;i<5;++i){
bank_customers[i].account_number,bank_customers[i].acco
unt_type, bank_customers[i].account_holder_name,
bank_customers[i].balance);
}
}
typedef struct {
char first_name[20];
char last_name[20];
} customer_name;
typedef struct {
int account_number;
char account_type[20];
customer_name account_holder_name;
double balance;
} bank_customer;
bank_customer bank_customers[5];
bank_customers[0].account_number = 1001;
strcpy(bank_customers[0].account_type, "Checking");
strcpy(bank_customers[0].account_holder_name.first_name
, "John");
strcpy(bank_customers[0].account_holder_name.last_name,
"Paul");
bank_customers[1].account_number = 1002;
strcpy(bank_customers[1].account_type, "Checking");
strcpy(bank_customers[1].account_holder_name.first_name
, "Mary");
strcpy(bank_customers[1].account_holder_name.last_name,
"Paul");
bank_customers[1].balance = 30100.50;
for(int i=0;i<5;++i){
printf("Account Number: %d\nAccount Type:
%s\nACcount Holder fist Name: %s\nCcount Holder Last
Name: %s\nbalance: %.2lf\n-------------
\n",bank_customers[i].account_number,bank_customers[i].a
ccount_type,
bank_customers[i].account_holder_name.first_name,
bank_customers[i].account_holder_name.last_name,
bank_customers[i].balance);
}
}
struct student
{
int id;
char name[20];
char grade;
};
int main()
{
struct student astud;
astud.id=9401;
strcpy(astud.name, "Joe");
astud.grade = 'A';
func(astud);
return 0;
}
Abdelghani Bellaachia, CSCI 1121 Page: 15
void func(struct student astud)
{
printf(" Id is: %d \n", astud.id);
printf(" Name is: %s \n", astud.name);
printf(" Grade is: %c \n", astud.grade);
}
8. Questions/Practice
Write a C program that implement complex
numbers (see solution below)
Write a C program that implement fractions (Lab)
Modify the last program in section 7 to include the
address of a student as a separate structure. The
address should include the following:
o Address as an array of 30 characters
o City as a an array of 20 characters
o Zipcode as an integer.
Complex numbers:
//gcc 5.4.0
#include <stdio.h>
typedef struct {
double realpart;
double imaginarypart;
} complex;
return (addc);
}
complex subcomp (complex a, complex b){
complex addc;
addc.realpart = a.realpart - b.realpart;
addc.imaginarypart = a.imaginarypart - b.imaginarypart;
return (addc);
}
int main(void) {