0% found this document useful (0 votes)
21 views17 pages

(CS174) Structures and Union (From Mahundi)

Here are the steps to declare a structure, array of structures and assign values: 1. struct grade { char regno[15]; float marks; }; 2. struct grade records[5]; 3. for(int i=0; i<5; i++) { printf("Enter registration number: "); scanf("%s", records[i].regno); printf("Enter marks: "); scanf("%f", &records[i].marks); } This declares a structure grade with members regno and marks. An array of 5 elements of type grade is declared. A for loop is used to prompt the user to enter values for each element of the array

Uploaded by

frankkinunghi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views17 pages

(CS174) Structures and Union (From Mahundi)

Here are the steps to declare a structure, array of structures and assign values: 1. struct grade { char regno[15]; float marks; }; 2. struct grade records[5]; 3. for(int i=0; i<5; i++) { printf("Enter registration number: "); scanf("%s", records[i].regno); printf("Enter marks: "); scanf("%f", &records[i].marks); } This declares a structure grade with members regno and marks. An array of 5 elements of type grade is declared. A for loop is used to prompt the user to enter values for each element of the array

Uploaded by

frankkinunghi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIVERSITY OF DAR ES SALAAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS 174: PROGRAMMING IN C

Masoud H. Mahundi
Structures and Unions
Introduction
 Appropriate when we need to manage data of different data type
 Students
 name, registration number, date of birth
 Address
 house number, street, zip code, country
 Books
 Author, pages, ISBN, Tittle, year
Structure - Definition
 Structure Definition is basically creating a temporary data type to later be used in
declaration

 Definition - you specify the different data types included in structure data type

struct structure_name
{
data_type member1;
data_type member2;
……..
data_type memberN;
};
struct books
{
char title[20]; Data type: struct books
char author[15];
int pages;
float price;
};

tittle author pages price

struct address {
unsigned int house_number;
char street_name[50];
Data type: struct address
int zip_code;
char country[50];
};
Structure - Declaration
1. Immediately after definition
struct books struct books
{ {
char title[20]; char title[20];
char author[15]; char author[15];
int pages; int pages;
float price; float price;
} book1; } book1, book2;

2. As other data type declarations


struct books book1;

struct books book1, book2;


Structure - Initialisation
1. As a set
struct books
 struct books book1 = {“Programming in C”, “Mahundi”, 97, 5000}; {
char title[20];
2. Separate members char author[15];

 struct books book1; int pages;


float price;
 book1.title = “Programming in C”;
};
 book1.pages = 97;

 book1.author = “mahundi”;

 book1.price = 5000; Accessing members


of a structure
SELF TEST
1. Define a structure named grade with the following members
1. regno of type string with 15 characters

2. marks of type float;


struct structure_name
2. Assign the following values {

1. registration number to be 2016-06-0976 data_type member1;


data_type member2;
2. Marks to be 78.5
……..
data_type memberN;
};
#include<stdio.h>
struct computer
{
char name[30];
int stock;
float price, discount;
}; //notice the semi-colon after the structure definition
main()
{
//assining values to the variable p1 of type struct computer definition
struct computer p1 ={"Dell Inspiron, Intel Core", 35,298.56, 2.32};
printf("Name = %s\n",p1.name);
printf("Stock = %d\n",p1.stock);
printf("Price = %.2f\n",p1.price);
printf("Discount= %.2f\n",p1.discount);
}
Structure - Nesting
Struct structureOne
{
int age;
float height;
};
Struct structureTwo
“customer” carries
{ two members; age
and height
double salary;
struct structureOne customer;
};
Structure - Nesting
struct structureone
{
int age;
float height;
};
struct structuretwo
{
double salary;
Accessing Nested
struct structureone customer; members
};
struct structureTwo transa;
transa.salary = 12345.6789;
transa.customer.age = 5;
transa.customer.height = 1023.17;
SELF TEST

struct student_some_detail
{
int college_id; Write scanf statements to
char college_name[50];
access all the members of
};
stu_data variable
struct student_detail
{
int id;
char name[20];
float percentage;
struct student_some_detail clg_data;
}stu_data;
Structure - Arrays
 As a normal array
struct student
 Data type being the structure defined {
char reg_no[12];
int values[5];
float allowance;
float marks[200]; char sex;

struct student thestudent[200]; };


struct student coict[5];
Structure - Arrays
struct student
{
char reg_no[12];
float allowance;
char sex;
};
struct student coict[5];

reg_no allowance sex


reg_no allowance sex
reg_no allowance sex
reg_no allowance sex
reg_no allowance sex
Accessing members
struct student
{
char reg_no[12];
float allowance;
char sex; assignment members
};
coict[0].reg_no =
struct student coict[5];
coict[0].allowance =
coict[0].sex =
coict[1].reg_no =
coict[1].allowance =
coict[1].sex =
SELF TEST
1. Define a structure named grade with the following members
1. regno of type string with 15 characters
2. marks of type float;

2. Declare an array of 5 elements whose type is the structure in 1


3. Assing values to the elements of the array you have declared above
4. Use for-loop to prompt and accept values from the user to enter into the
elements of the structure you have declared above

You might also like