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

Programming and Problem Solving Module2

The document discusses different aspects of structures in C programming including: 1. Defining and using basic structures to store collections of different data types. 2. Declaring arrays of structures to store information about multiple entities. 3. Passing individual structure members and entire structures to functions. 4. Using structure pointers to point to structures in memory and access members. 5. Using bit fields to define individual bits within structures to reduce memory usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Programming and Problem Solving Module2

The document discusses different aspects of structures in C programming including: 1. Defining and using basic structures to store collections of different data types. 2. Declaring arrays of structures to store information about multiple entities. 3. Passing individual structure members and entire structures to functions. 4. Using structure pointers to point to structures in memory and access members. 5. Using bit fields to define individual bits within structures to reduce memory usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAMMING AND PROBLEM SOLVING

(MODULE 2- STRUCTURE & UNIONS)


PROGRAMMING & PROBLEM SOLVING (MODULE 2)
1. STRUCTURE :
Structure in c is a user-defined data type that enables us to store the collection of different data types.
[Simple Structure program / (Demo C program that In certain circumstances use of structure is better than array)]
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
2. ARRAY OF STRUCTURE :
An array of structres in C can be defined as the collection of multiple structures variables where each variable contains information about
different entities.
Example :
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
3. PASSING STRUCTURE TO FUNCTIONS :
We can pass structure in two ways : 1. Passing Structure Members to Functions 2. Passing Entire Structures to Functions
3.1 Passing Structure Members to Functions
When we pass a member of a structure to a function, we are passing the value of that member to the function.
struct fred
{
char x;
int y;
float z;
char s[2];
} mike;

Here each member being passed to a function :


func(mike.x); /* passes character value of x */
func2(mike.y); /* passes integer value of y */
func3(mike.z); /* passes float value of z */
func4(mike.s); /* passes address of string s */
func(mike.s[2]); /* passes character value of s[2] */
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
3. PASSING STRUCTURE TO FUNCTIONS :
3.2 Passing Structure Entire Members to Functions
When a structure is used as an argument to a function, the entire structure is passed using the normal call-by-value method.
Example :
#include<stdio.h>
struct add
{
int var1;
int var2;
}a;
void show(struct add a)
{
int sum;
sum=a.var1+a.var2;
printf("Added value is %d",sum);
}
void main()
{
struct add a;
printf("Enter variable 1 = ");
scanf("%d",&a.var1);
printf("Enter variable 2 = ");
scanf("%d",&a.var2);
show(a);
}
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
4. STRUCTURE POINTER :
The pointer which points to the address of the memory block that stores a structure is known as the structure pointer.
How a structure pointer is used, shown in the below program,
#include <stdio.h>
struct Employee
{
char name[30];
int id;
int age;
char gender[30];
char city[40];
};
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
4. STRUCTURE POINTER :
struct Employee emp1,*ptr1;
int main()
{
ptr1 = &emp1;
printf (" Enter the name of the Employee (emp1): ");
scanf (" %s", &ptr1->name);
printf (" Enter the id of the Employee (emp1): ");
scanf (" %d", &ptr1->id);
printf (" Enter the age of the Employee (emp1): ");
scanf (" %d", &ptr1->age);
printf (" Enter the gender of the Employee (emp1): ");
scanf (" %s", &ptr1->gender);
printf (" Enter the city of the Employee (emp1): ");
scanf (" %s", &ptr1->city);

printf ("\n Display the Details of the Employee1 using Structure Pointer");
printf(" Name: %s", ptr1->name);
printf(" Id: %d", ptr1->id);
printf(" Age: %d\n", ptr1->age);
printf(" Gender: %s\n", ptr1->gender);
printf(" City: %s\n", ptr1->city);
return 0;
}
PROGRAMMING & PROBLEM SOLVING (MODULE 2)
5. BIT FIELDS :
 C has a built-in feature called a bit-field that allows us to access a single bit.
Example :
#include <stdio.h>
#include <string.h>
struct
{
unsigned int widthValidated;
unsigned int heightValidated;
} status1;
struct
{
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status2;
int main( )
{
printf( "Memory size occupied by status1 : %d", sizeof(status1));
printf( "Memory size occupied by status2 : %d", sizeof(status2));
return 0;
}

You might also like