Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Lecture 6:

Structured Data Types

1
Arrays
Strings
Structures
Unions
2
Arrays
3

 Array is a data structure that hold finite sequential


collection of homogeneous data.
 Is a collection of data items, all of the same type,
accessed using a common name.
 The elements of an array are differentiated from one
another by their position within an array.
 Arrays consist of contiguous memory locations.
Arrays Indexing
4

 A specific element in an array is accessed by an index.


 Arrays in C are indexed starting at 0.

• list[0] - 1st element = 10


• list[1] - 2nd element = 20
• …
• list[4] - 5th element = 50
Arrays Declaration
5

 The general form of declaring a array is:


datatype arrayName[size];
Arrays Initialization
6
Arrays Initialization…
7
Arrays Initialization…
8

 The size and type of an array cannot be changed once it is


declared

 The number of elements should not exceed the array size

 You should never access elements of an array outside of its


bound
 Accessing an element that does not exist may result into
undefined behavior
Accessing Array Elements
9

 Array elements are accessed by using index


 Array indexing starts at zero

 Know how to declare an array


 Know how to initialize an array
 Know how to access array elements
Exercise 1
10

Write a C program which stores five


numbers 10, 20, 30, 40, and 50 in an array.
The program should finally display those
numbers by using a for loop.
11
Exercise 2
12

Write a C program which stores five


numbers 10, 20, 30, 40, and 50 in an array.
The program should finally calculate and
display the sum of all numbers
13
Arrays
Strings
Structures
Unions
14
Strings
15

 A string is an array of characters.


 The C language does not provide an inbuilt data type for strings
 A compiler automatically adds a null character (‘\0’) at end of a string.
String Output
16

 String data can be printed to the screen by using printf():


printf(“%s”, name);

 Another function for displaying String variable is puts():


puts(name);

 Another function for displaying String variable is fputs():.


fputs(name, stdout);
String Input
17

 String input can be performed using scanf():


scanf(“%s”, name);

 Another function for String input is gets():


gets(name);
 Another function for String input is fgets():
fgets(name, MAX_LEN, stdin);
Strings Input/Output
18

# include <stdio.h>
int main( ) {
char firstname[30];
printf(“What is your first name: ”);
scanf(“%s”, firstname);
printf(“Your first name is %s \n”, firstname);
}
Strings Input/Output
19

# include <stdio.h>
int main( ) {
char firstname[30];
printf(“What is your first name: ”);
gets(firstname);
printf(“Your first name is %s \n”, firstname);
}
Strings Input/Output
20

# include <stdio.h>
int main( ) {
char firstname[30];
printf(“What is your first name: ”);
fgets(firstname, 30, stdin);
printf(“Your first name is %s \n”, firstname);
}
Strings Input/Output…
21

# include <stdio.h>
int main( ) {
char firstname[10], surname[10];
printf(“What is your first name: ”);
scanf(“%s”, firstname);
printf(“What is your surname name: ”);
scanf(“%s”, surname);
printf(“Your full name is %s %s\n”, firstname, surname);
}
String Handling Functions
22

 C language provides various functions to manipulate


strings.
 These functions are present inside string.h library.
 You must include string.h header file in your
programs when using these functions.
String Handling Functions
23

Function Purpose
strlen() Returns the length of the string
strlwr() Converts a string to lowercase
strupr() Converts a string to uppercase
strrev() Shows a string in reverse order
strcpy(str1, str2) Copies string str2 into string str1
strcmp(str1, str2) Compares two strings
strcat(str1, str2) Concatenates (combines) two strings
Strings Handling Functions…
24

# include <stdio.h>
# include <string.h>
int main( ) {
char name[6] = “Deus”;
printf(“%d \n”, strlen(name));
printf(“ %s \n”, strlwr(name));
printf(“ %s \n”, strupr(name));
printf(“ %s \n”, strrev(name));
}
Strings Handling Functions…
25

# include <stdio.h>
# include <string.h>
int main( ) {
char firstname[6] = “Deus”;
char surname[6] = “Msaki”;

printf(“My full name is %s \n”, strcat(firstname, surname));


}
Strings Handling Functions…
26

# include <stdio.h>
# include <string.h>
int main( ) {
char firstname[20] = “Dominick”;
char surname[20];
strcpy(surname, firstname);
printf(“My surname name is %s \n”, surname);
}
Strings Handling Functions…
27

# include <stdio.h>
# include <string.h>
int main( ) {
char firstperson[10] = “Deus”;
char secondperson[10] = “Zacharia”;
int c = strcmp(firstperson, secondperson);
if (c == 0)
printf(“The two names are identical\n”);
else
printf(“The two names are NOT identical \n”);
}
Arrays
Strings
Structures
Unions
28
Structures
29

 Structure is a user-defined data type in C language which


allows us to store collection of different data types together.
 Structure is somewhat similar to an array, but an array holds
data of similar type only.
 In a structure, data is stored in form of records.
 Each element of a structure is called a member.
Defining a Structure
30

 struct keyword is used to define a structure.


 struct defines a new data type (collection of primary & derived
data types.
Defining a Structure…
31

 A structure definition for storing student records:


Defining a Structure…
32

 A structure definition for storing employee information:


Declaring a Structure Variable
33

 There are two ways to declare a structure variable:

 Declaring structure variables during structure definition

 Declaring structure variables after structure definition


Approach 1…
34

 Declaring structure variables during structure definition


Approach 2…
35

 Declaring structure variables after structure definition


First we define a structure Then we declare variables
after definition
Declaring a Structure Variable…
36

 Which approach is good?


 If the number of structure variables is fixed, use the 1st
approach. It saves your code/space.
 If the number of structure variables is not known, the
2nd approach is better. It provides the flexibility to
declare the structure variables many times as required.
Accessing Members of a Structure
There are two ways to access structure members
(.) member or dot operator (->) structure pointer operator

37
#include <stdio.h>
struct Student {
int regno;
float marks;
char grade;
};

int main(){
struct Student omary;
omary.regno = 2020;
omary.marks = 68;
omary.grade = 'B';
38 }
Structure Initialization
39

Approach A Approach B struct Student {


int regno;
struct Student float marks;
{ char grade;
int regno; };
float marks;
char grade;
}; struct Student omary;
omary.regno = 2020;
struct Student omary = { 2020, 68, 'B' }; omary.marks = 68;
omary.grade = 'B';
Structure Initialization…
40

struct Student { Approach C


int regno;
float marks;
char grade;
};

struct Student omary;


printf("Enter student reg. number: "); scanf("%d", &omary.regno);
printf("Enter student marks: "); scanf("%f", &omary.marks);
printf("Enter student grade: "); scanf("%c", &omary.grade);
Structure Output
41

#include <stdio.h>
struct Student {
int regno;
float marks;
};
int main(){
struct Student omary;
omary.regno = 2020;
omary.marks = 68;
printf("The student reg. number is: %d \n", omary.regno);
printf("The student marks is: %f \n", omary.marks);
}
Structures

Unions

Functions

42
Exercise 2
43

 Write a C program which stores five numbers


in an array. The program should calculate the
average of those numbers and display the
result.
# include <stdio.h>
int main( ) {
int number[5] = {10, 20, 30, 40, 50};
int sum = 0;
for( int i = 0; i < 5; i++)
{
sum = sum + number[i];
}
float average = sum / 5;
printf(“The average of five numbers is %f”, average);
}
44

You might also like