Introduction to Computing and Programming
Structure, Union & Bit Manipulation
Content
• Recap
• User-defined Vs Derived data Types
• Structure
• Union
• Bit Manipulation
Recap
• String Function
• String Pointers
• Array of Pointers to String
User-defined Vs Derived data Types
• User-defined and Derived data types both extend the basic data
types, but they differ in the declaration and usage.
• User-defined data types are created using fundamental data types and
custom structures are tailored to specific needs. Eg: Structure,
Union, etc.
• Derived data types are created from existing data types but don’t
involve creating new types.
• They extend the behavior or structure of existing data types rather than
defining entirely new types. Eg: arrays, pointers, functions, etc.
User-defined Vs Derived data Types Cont..
• Arrays contains elements of the same data
type.
• Sometimes, it is required to group information
of different data types (Example: Student
Why Details)
Structures?
• In C, we can store combinations of character,
integer floating point and enumerated type
data (explained later). They are called structs.
What is Structures?
• Definition: A structure is a user-defined data type that groups related
variables of different data types under a single name.
• Syntax: struct structureName {
dataType1 member1;
dataType2 member2;
...
};
• Purpose: Useful for organizing complex data, like representing a
student’s information with name, age, and grades.
Example of defining Structure in C
▪ Example: struct Student {
char name[50];
int age;
float grade;
};
struct Student s1;
▪ Explanation: struct Student defines a structure named Student. s1 is a
variable of this type with name, age, and grade fields.
Two ways to declare variables of a struct
1. We can declare variables of type 2. We can declare variables of type
struct right at the place of struct in another place we need:
declaration: struct Student {
struct Student { char name[50];
char name[50]; int age;
int age; float grade;
};
float grade;
int main(void){
}s1; /* we declare a variable s1*/ struct Student s1;
}
Accessing Structure Members
• Accessing Members: Use the dot (.) operator with structure variables.
• Example: s1.age = 20;
printf("Age: %d", s1.age);
▪ Explanation: Assigns and prints the age of s1.
Example of C program using Struct
#include <stdio.h> Output:
#include <string.h>
typedef struct Students{
int rollno;
char name[5];
}Student;
int main() {
Student stud1;
stud1.rollno = 1;
strcpy(stud1.name, "John");
printf("Student rollno: %d\n", stud1.rollno);
printf("Student name: %s\n", stud1.name);
return 0;}
typedef
• typedef is used for creating synonyms for previously defined data
type names.
• E.g.: typedef int Length;
• ‘Length’ is used as a synonym (or alias) for the data type int.
• Now ‘Length’ be used in declarations in the same way that the data
type int can be used:
Length a, b, len ;
Length numbers[10] ;
Typedef & Struct
• Often, typedef is used in combination with struct to declare a
synonym (or an alias) for a structure:
typedef struct Students
{
int roll_no; /* assume integer roll no */
char name[20]; } Student;
/* We can use Student like any other type*/
int main(void)
{ Student stud1; }
Array of Structures
• Just like any other type you can declare an array of struct
Student stud[10];
• The above declares an array of size 10.
• As any other array, the storage in memory is contiguous
Example of Array of Structures
#include <stdio.h> Output:
#include <string.h>
typedef struct Students{
int rollno;
char name[5];}Student;
int main() {
Student stud[2];
stud[0].rollno = 1;
strcpy(stud[0].name, "John");
stud[1].rollno = 2;
strcpy(stud[1].name, "Mary");
for(int i=0; i <2; i++){
printf("Student rollno: %d\n", stud[i].rollno);
printf("Student name: %s\n", stud[i].name); }
return 0;}
Pointers to structures
• Like declaring pointers to integers, double, etc. We can declare
pointers to structs
• int* p1;
• float* p2;
• Student* p3;
• The pointer p3 stores the address to a struct and works exactly like
pointers to other data types
• We can access the values as follows
(* p3).roll_no
Example of Pointer to Structure
#include <stdio.h> Output:
#include <string.h>
typedef struct Students{
int rollno;
char name[5];
}Student;
int main() {
Student stud1;
stud1.rollno = 1;
strcpy(stud1.name, "John");
Student* p1;
p1 = &stud1;
printf("Student rollno: %d\n", (*p1).rollno);
printf("Student name: %s\n", (*p1).name);
return 0;}
The ‘->’ shorthand operator
• We can access the values as using the shorthand -> operator
• The below two statements have the same meaning
(* p3).roll_no;
p3->roll_no;
Example of Pointer to Structure
#include <stdio.h> Output:
#include <string.h>
typedef struct Students{
int rollno;
char name[5];
}Student;
int main() {
Student stud1;
stud1.rollno = 1;
strcpy(stud1.name, "John");
Student* p1;
p1 = &stud1;
printf("Student rollno: %d\n", p1->rollno);
printf("Student name: %s\n", p1->name);
return 0;}
Unions
• Definition: A union is similar to a structure but shares memory for all
its members.
▪ Syntax:
union unionName {
dataType1 member1;
dataType2 member2;
...
};
▪ Purpose: Useful when variables don’t need to exist simultaneously,
conserving memory.
Example of Union in C
Example: union Data {
int intVal;
float floatVal;
char charVal;
};
union Data d1;
• Explanation: union Data defines a union with an integer, float, and
char sharing the same memory location.
Example of C program using union
#include <stdio.h> Output:
#include <string.h>
union Student {
int roll_no;
char name[50];
};
int main() {
union Student student;
student.roll_no = 12345; // Assign and display roll number
printf("Roll No: %d\n", student.roll_no);
strcpy(student.name, "Alice Johnson"); // Assign and display name
printf("Name: %s\n", student.name);
printf("Roll No after assigning name: %d\n", student.roll_no); // Print roll_no after name
assignment, Undefined value
return 0;
}
Structures vs. Unions
Bit Manipulation: Operators
Note: Only defined for int and char values, not for float or double
ICP Project Discussion
• Compulsory
• We will float a Google form to fill the details of group & project title
• Team of 2 students (max)
• Plag check would be there
• Mostly deadline would be Sunday midnight; (24th Nov 2024, 11:59pm)
• Submit 1-to-2-page report with your observations as well along with
code
• Note: You can use structure data type, array, loops, pointers, functions,
recursion, etc to solve above problem.
Announcement: Quiz 3 Discussion
• Quiz 3 is on 21st Nov (Thursday): from 12:30pm to 1pm.
• Syllabus will be everything that we have covered till today, including
Structure, Union & Bit manipulation.
• 5 to 6 questions: MCQs, Short answer questions & coding question.
• ~ 15 marks
Upcoming Slides
• File Streaming and Processing
• Introduction to Data Structures