[PDF] [PDF] C Structures - GWU SEAS

Page: 1 C Structures 1 Example: o The following structure has three fields: typedef struct { int day; int month; We would like to write a C program that stores



Previous PDF Next PDF





[PDF] STRUCTURES IN C PROGRAMMING

Then a variable of this structure type is declared and used in the program For example: struct date order_date; Note : When you first define a structure in a file, the 



[PDF] Structures in C - Tutorialspoint

structure is another user defined data type available in C programming, which The structure tag is optional and each member definition is a normal variable 



[PDF] C Programming Structure http://wwwprogramizcom/c-programming

Structure is the collection of variables of different types under a single name for better handling For example: You want to store the information about person 



[PDF] C Programming Tutorial

If you discover that the tutorialspoint com site or this tutorial C Program Structure C is a successor of B language, which was introduced around 1970



[PDF] structs

Example #include struct Box Box idth struct Box { int width; int length; in a struct CSE 251 Dr Charles B Owen Programming in C 5 in a struct



[PDF] C Structures - GWU SEAS

Page: 1 C Structures 1 Example: o The following structure has three fields: typedef struct { int day; int month; We would like to write a C program that stores



[PDF] C Programming for Engineers Structures, Unions

Card is a synonym for type struct card ➢ Example: ▫ typedef struct { char *face; char *suit; } 



[PDF] LECTURE NOTE on PROGRAMMING IN “C” - VSSUT

Lecture 2: Structure of C, compilation, execution Lecture 3:character differently , for example code, Code, CODE etc are different identifiers 5) identifiers are 



[PDF] Fundamentals of C Structure of a C Program

Structure of a C Program #include copies a file into the source code #include d int d none hd short int d h c char c none Example Type Code Size 



[PDF] C & Data Structures - Dhurina

The array in this example is a data structure, and the for loop, used for sequential access to the array, executes a simple algorithm For uncomplicated programs 

[PDF] student accommodation leicester

[PDF] student assistance team checklist

[PDF] student attribute restriction

[PDF] student database management system project in sql

[PDF] student health forms

[PDF] student health services immunization

[PDF] student immunization form vcu

[PDF] student internship program

[PDF] student journals

[PDF] student record keeping system database project

[PDF] student research proposal example pdf

[PDF] student solutions manual to accompany complex variables and applications

[PDF] student visa france

[PDF] student writing samples with errors

[PDF] students can modify the documents in the class notebook content library

Abdelghani Bellaachia, CSCI 1121 Page: 1

C 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

Abdelghani Bellaachia, CSCI 1121 Page: 2

1. Objective

What is C structure?

When to use structures.

Syntax of a structure.

How to declare variable of type structure?

Fields of a structure and how to initialize them.

How to manipulate structure type

2. Structure Definition

A Structure is a collection of related data items, possibly of different types.

Structures are also called records.

A structure type in C is called struct.

Unlike arrays, a struct is composed of data of different types. You use structures to group data that belong together.

Examples:

o Student information:

ƒ student id,

ƒ last name,

ƒ first name

ƒ major,

ƒ gender,

Abdelghani Bellaachia, CSCI 1121 Page: 3

o Bank account information:

ƒ account number,

ƒ account type

ƒ account holder

first name last name

ƒ balance

Data elements in a structure are called fields or

members

Complex data structures can be formed by defining

arrays of structs.

3. Struct Syntax

Syntax of the structure type::

typedef struct{ type1 id1; type2 id2; } struct_name ;

Required

Abdelghani Bellaachia, CSCI 1121 Page: 4

Example:

o The following structure has three fields: typedef struct { int day; int month; int year; } eventDate; typedef struct { char name[20]; int age; } person; struct telephone { char name[30]; int number;

Abdelghani Bellaachia, CSCI 1121 Page: 5

How to declare variable of type structure?

o Create a variable, var1, using this structure: typedef struct{ type1 id1; type2 id2; } struct_name ; sturct_name var1; o Examples: person p1; person p2;

Abdelghani Bellaachia, CSCI 1121 Page: 6

4. Manipulating Structure Types

How to access a field in a structure:

o Use the direct component selection operator, which is a period. o The direct component selection operator has the highest priority in the operator precedence. o Examples: person p1; p1.name; p1.age;

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.

Abdelghani Bellaachia, CSCI 1121 Page: 7

o Examples:

ƒ Given the following structure:

typedef struct { int day; int month; int year; } eventDate; eventDate ev1; ev1.day = 2; ev1.month = 11; ev1.year = 2017;
eventDate ev2 = { 20, 10, 2017};

ƒ Given the following structure

typedef struct { char name[20]; int age; } person; person p2; strcpy(p2.name, "Paul p2.age = 27;

Abdelghani Bellaachia, CSCI 1121 Page: 8

5. Arrays of Structures

We can also declare an array of structures.

Recall the syntax of an array:

type array_name[size]; type can any C type including struct type. The array of structures can be simply manipulated as arrays of simple data types.

Example:

typedef struct { char name[20]; int age; } person; person class[5]; class[0].age = 19; class[1].age = 18; .name .age

Abdelghani Bellaachia, CSCI 1121 Page: 9

class[0] John 19 class[1] Sara 18 class[2] David 20 class[3] Mary 21 class[4] Paul 18

Application: Bank Account

We would like to write a C program that stores

customers at a bank.

Here are the different steps:

ƒ Step 1: design your structure

typedef struct { int account number; char account_type[20]; char account_holder_name[40]; double balance; } bank_account;

ƒ Step 2: declare the array of structs

bank_customer bank_customers[100]; class[3].age class[4].name

Abdelghani Bellaachia, CSCI 1121 Page: 10

ƒ Step 3: Initialization

bank_customers[0].account_number = 1001; strcpy(bank_customers[0].account_type, "Checking"); e, "John Paul"); bank_customers[0].balance = 2100.50;

ƒ Full Program:

//gcc 5.4.0 #include int main(void) typedef struct { int account_number; char account_type[20]; char account_holder_name[40]; double balance; } bank_customer;

Abdelghani Bellaachia, CSCI 1121 Page: 11

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, "John

Paul");

bank_customers[0].balance = 2100.50; for(int i=0;i<5;++i){ printf("Account Number: %d\nAccount Type: %s\nACcount Holder Name: %s\nbalance: %.2lf\n----------- --\n", unt_type, bank_customers[i].account_holder_name, bank_customers[i].balance);

Abdelghani Bellaachia, CSCI 1121 Page: 12

6. Hierarchical structure

A hierarchical structure is a structure containing components which are also structures. Let us review the description of the bank account of a customer: o Here is the initial definition: typedef struct { int account number; char account_type[20]; char account_holder_name[40]; double balance; } bank_account; o Now, let us break the name of a customer into:

ƒ First name

ƒ Last name

o Let us define a name structure as follows: typedef struct { char first_name[20]; char last_name[20]; } customer_name;

Abdelghani Bellaachia, CSCI 1121 Page: 13

o Here is the updated program: //gcc 5.4.0 #include #include int main(void) 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"); , "John"); "Paul");

Abdelghani Bellaachia, CSCI 1121 Page: 14

bank_customers[0].balance = 2100.50; bank_customers[1].account_number = 1002; strcpy(bank_customers[1].account_type, "Checking"); , "Mary"); "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-------------

ccount_type, bank_customers[i].balance);

Abdelghani Bellaachia, CSCI 1121 Page: 15

7. Function with a Structure Input Parameter

When a structure variable is passed as an input

argument to a function, all its component values are copied into the local structure variable.

Example:

#include struct student int id; char name[20]; char grade; void func(struct student stud); int main() struct student astud; astud.id=9401; strcpy(astud.name, "Joe"); astud.grade = 'A'; func(astud); return 0;

Abdelghani Bellaachia, CSCI 1121 Page: 16

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.

Abdelghani Bellaachia, CSCI 1121 Page: 17

9. Partial Solution

Complex numbers:

//gcc 5.4.0 #include typedef struct { double realpart; double imaginarypart; } complex; complex addcomp (complex a, complex b){ complex addc; addc.realpart = a.realpart + b.realpart; addc.imaginarypart = a.imaginarypart + b.imaginarypart; 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) {

Abdelghani Bellaachia, CSCI 1121 Page: 18

complex c1; complex c2; complex c3; c1.realpart = 2.3; c2.realpart = 2.3; c1.imaginarypart = 2.3; c2.imaginarypart = 2.3; c3 = addcomp(c1, c2); printf("%.2lf+%.2lfi + %.2lf+%.2lfi = %.2lf+%.2lfi\n", c1.realpart, c1.imaginarypart,c2.realpart, c2.imaginarypart,c3.realpart, c3.imaginarypart); c1.imaginarypart = 2.3; c2.imaginarypart = 2.3; c3 = subcomp(c1, c2); printf("%.2lf+%.2lfi - %.2lf+%.2lfi = %.2lf+%.2lfi\n", c1.realpart, c1.imaginarypart,c2.realpart, c2.imaginarypart,c3.realpart, c3.imaginarypart); return 0;quotesdbs_dbs14.pdfusesText_20