0% found this document useful (0 votes)
11 views

Ch10-Structure C-Style

The document discusses structures in C programming. It defines a structure as a complex data type made up of other data types. Structures allow grouping of related data and can contain arrays of other structures. The key points covered are: 1. Defining structures with tags and member definitions. 2. Declaring and initializing structure variables. 3. Accessing members of a structure using the dot operator. 4. Declaring arrays of structures to store multiple records. 5. Sample programs demonstrating single and nested structures, arrays of structures, and printing structure member values.

Uploaded by

asdfasdfas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Ch10-Structure C-Style

The document discusses structures in C programming. It defines a structure as a complex data type made up of other data types. Structures allow grouping of related data and can contain arrays of other structures. The key points covered are: 1. Defining structures with tags and member definitions. 2. Declaring and initializing structure variables. 3. Accessing members of a structure using the dot operator. 4. Declaring arrays of structures to store multiple records. 5. Sample programs demonstrating single and nested structures, arrays of structures, and printing structure member values.

Uploaded by

asdfasdfas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Introduction to Programming

FSC1331/FDN1120
Topic 10 : Structure (C-Style Struct)

Topic’s / Unit’s Learning Outcome:


1. Structures
2. Accessing Structures
3. Array of Structures
4. Sample Programs

17/01/2016 1
10.1 Structures

• A structure is a complex data type made up of other data


types.
• For example, we could combine the three strings (char arrays)
for name, address, and phone number into one structure.
• We may access the entire structure – move it into secondary
storage, for example; or we could access an individual
member of the structure – change the address, for example.

17/01/2016 2
10.1.1 Definitions and Declarations

• Structures are data types that we make up ourselves. Their


components are the existing data types.
• Working with a structure requires a step that is already done
for us with the standard data types – providing a structure
definition, where we tell C the makeup of the structure.

17/01/2016 3
10.1.1 Definitions and Declarations

• The tag is the name of new data type. It is equivalent of float


or int and is used in much the same way – we will declare
variables of that data type to be used in the program.
• In the member definitions we will define each of the
individual variables that make up the structure.

17/01/2016 4
10.1.1 Definitions and Declarations

• The structure definition only tells C the makeup of the


structure. It does not allocate memory.
• To use the new data type, we will have to declare variables of
that type.

17/01/2016 5
10.1.1 Definitions and Declarations

• Structure tags are defined


externally, outside of a function,
are visible globally, in fact, the
structure can be declared
anywhere in the program.
• It is often a good idea to define
structure externally, where they
are visible to all functions.
• C also allows us to both define and
declare structures in the same
statement.
17/01/2016 6
10.1.2 Initialization

• We may also explicitly initialize structures at the time of their


declarations.
• The important criterion for initializations is that the values
are listed in the exact same order as the members in the
structure.
• The entire definition, declaration, and initialization could be
contained in one statement.

17/01/2016 7
10.2 Accessing Structures

• Structures are most often accessed – read or assigned – by


accessing individual members.
• Since we may have many variables of the same structure
type and each of these variables will have the same member
names, we must tie the member name to the specific
structure variable with a member operator, a dot (.).

17/01/2016 8
10.2 Accessing Structures

17/01/2016 9
10.3 Arrays of Structures

• We may declare an array of structure base on the number of


records that we would like to store.

17/01/2016 10
10.3 Arrays of Structures

• To access the basicPay of the second staff in the array we


would refer to staff[1].basicPay.
• To access the third character of the name member of the
second staff we would refer to staff[1].name[2].
• The array may be initialized by putting a block of values after
the declaration

17/01/2016 11
10.4 Sample Programs

17/01/2016 12
10.4 Sample Programs

17/01/2016 13
10.4 Sample Programs

null terminating
character

17/01/2016 14
#include <stdio.h>
/* print Book1 info */
#include <string.h>
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
struct Books {
printf( "Book 1 subject : %s\n", Book1.subject);
char title[50];
printf( "Book 1 book_id : %d\n", Book1.book_id);
char author[50];
/* print Book2 info */
char subject[100];
printf( "Book 2 title : %s\n", Book2.title);
int book_id; };
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
int main( )
printf( "Book 2 book_id : %d\n", Book2.book_id);
{
return 0;
struct Books Book1;
}
struct Books Book2;

strcpy( Book1.title, "C Programming");


strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
Single Structure,
strcpy( Book2.title, "Telecom Billing"); Multi Structure Object
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
17/01/2016 15
#include <stdio.h>
#include <string.h>
struct stu_address {
char street[30]; Multi Structure,
char state[30];
char city[30]; Single Structure Object
char country[30]; };
struct stu_data {
int stu_id;
int stu_age;
char stu_name[20];
struct stu_address stuAddress; };
int main() {
struct stu_data mydata;
mydata.stu_id = 1001;
mydata.stu_age = 30;
strcpy(mydata.stuAddress.state, "perak"); //Nested struct assignment
strcpy(mydata.stuAddress.country, "Malaysia");
strcpy(mydata.stuAddress.street, "109");
strcpy(mydata.stuAddress.city, "ipoh");
printf("State: %S\n", mydata.stuAddress.state);
printf(“Country:%s\n", mydata.stuAddress.country);
printf(“Street:%s\n", mydata.stuAddress.street);
printf(“City:%s\n", mydata.stuAddress.city);
return 0;
}
16
#include <stdio.h>
#include <string.h>
struct student { int id; char name[30]; float percentage; };
int main() {
int i;

struct student record[3];


record[0].id=1; Array of Structure
strcpy(record[0].name, "Raju");
record[0].percentage=86.5;
Single Structure,
record[1].id=2; Single Structure Object
strcpy(record[1].name, "Ahmad");
record[1].percentage=70.5; using array
record[2].id=3;
strcpy(record[2].name, "Kin Fai");
record[2].percentage=80.5;

for(i=0; i<3; i++) {


printf(" Record of student: %d \n", i+1);
printf("ID is : %d \n", record[i].id);
printf("Name is : %s \n", record[i].name);
printf("Percentage is : %f \n\n", record[i].percentage);
}
return 0;
}
17

You might also like