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

UNIT-4

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

UNIT-4

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

POINTERS

The pointer in C language is a variable which stores the address of another variable. This
variable can be of type int, char, array, function, or any other pointer. The size of the pointer
depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an integer.
1. int n = 10;
2. int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of typ
e integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer used to dereference a pointer.
1. int *a;//pointer to int
2. char *c;//pointer to char
Consider the following example to define a pointer which stores the address of an integer.
1. int n = 10;
2. int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of typ
e integer.
Pointer Example
An example of using pointers to print the address and value is given below.

#include<stdio.h>
int main( )
{ int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}
Output
Address of number variable is fff4

1
Address of p variable is fff4
Value of p variable is 50

Address Of (&) Operator


The address of operator '&' returns the address of a variable. But, we need to use %u to display
the address of a variable.
1. #include<stdio.h>
2. int main(){
3. int number=50;
4. printf("value of number is %d, address of number is %u",number,&number);
5. return 0;

Output

value of number is 50, address of number is fff4

Usage of pointer
There are many applications of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions where
the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code
and improves the performance.

STRUCTURES
Structure is a user-defined datatype in C language which allows us to combine data of different
types together. Structure helps to construct a complex data type which is more meaningful. It is
somewhat similar to an Array, but an array holds data of similar type only. But structure on the
other hand, can store data of any type, which is practical more useful.

For example: If I have to write a program to store Student information, which will have Student's
name, age, branch, permanent address, father's name etc, which included string values, integer
values etc, how can I use arrays for this problem, I will require something which can hold data of
different types together.

In structure, data is stored in form of records.


Defining a structure
struct keyword is used to define a structure. struct defines a new data type which is a collection
of primary and derived datatypes.

2
Syntax:
struct [structure_tag]
{
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];
As you can see in the syntax above, we start with the struct keyword, then it's optional to provide
your structure a name, we suggest you to give it a name, then inside the curly braces, we have to
mention all the member variables, which are nothing but normal C language variables of
different types like int, float, array etc.

After the closing curly brace, we can specify one or more structure variables, again this is
optional.
Example of Structure
struct Student
{
char name[25];
int age;
char branch[10];
// F for female and M for male
char gender;
};
Here struct Student declares a structure to hold the details of a student which consists of 4 data
fields, namely name, age, branch and gender. These fields are called structure elements or
members.
Each member can have different datatype, like in this case, name is an array of char type and age
is of int type etc. Student is the name of the structure and is called as the structure tag.

Declaring Structure Variables


It is possible to declare variables of a structure, either along with structure definition or after the
structure is defined. Structure variable declaration is similar to the declaration of any normal
variable of any other datatype. Structure variables can be declared in following two ways:

1) Declaring Structure variables separately


struct Student
{
char name[25];

3
int age;
char branch[10];
//F for female and M for male
char gender;
};
struct Student S1, S2; //declaring variables of struct Student
2) Declaring Structure variables with structure definition
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
}S1, S2;
Here S1 and S2 are variables of structure Student. However this approach is not much
recommended.
Accessing Structure Members
Structure members can be accessed and assigned values in a number of ways. Structure members
have no meaning individually without the structure. In order to assign a value to any structure
member, the member name must be linked with the structure variable using a dot . operator also
called period or member access operator.

For example:
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
int main()
{
struct Student s1;
s1.age = 18;
strcpy(s1.name, "Viraaj");

4
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);

return 0;
}
Name of Student 1: Viraaj
Age of Student 1: 18
We can also use scanf() to give values to structure members through terminal.
scanf(" %s ", s1.name);
scanf(" %d ", &s1.age);
Structure Initialization
Like a variable of any other datatype, structure variable can also be initialized at compile time.
struct Patient
{
float height;
int weight;
int age;
};
struct Patient p1 = { 180.75 , 73, 23 }; //initialization
or,
struct Patient p1;
p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;

Array of Structure
We can also declare an array of structure variables. in which each element of the array will
represent a structure variable. Example : struct employee emp[5];

The below program defines an array emp of size 5. Each element of the array emp is of type
Employee.

#include<stdio.h>

struct Employee
{
char ename[10];
int sal;
};

struct Employee emp[5];

5
int i, j;
void ask()
{
for(i = 0; i < 3; i++)
{
printf("\nEnter %dst Employee record:\n", i+1);
printf("\nEmployee name:\t");
scanf("%s", emp[i].ename);
printf("\nEnter Salary:\t");
scanf("%d", &emp[i].sal);
}
printf("\nDisplaying Employee record:\n");
for(i = 0; i < 3; i++)
{
printf("\nEmployee name is %s", emp[i].ename);
printf("\nSlary is %d", emp[i].sal);
}
}
void main()
{
ask();
}

Structure with in structure


When a structure contains another structure, it is called nested structure. For example,we have
two structures named Address and Employee. To make Address nested to Employee, we have to
define Address structure before and outside Employee structure and create an object of Address
structure inside Employee structure.
Syntax for structure within structure or nested structure
struct structure1
{
----------
----------
};

struct structure2
{
----------
----------
struct structure1 obj;

6
};
Example for structure within structure or nested structure

#include<stdio.h>

struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};

struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};

void main()
{
int i;
struct Employee E;

printf("\n\tEnter Employee Id : ");


scanf("%d",&E.Id);

printf("\n\tEnter Employee Name : ");


scanf("%s",&E.Name);

printf("\n\tEnter Employee Salary : ");


scanf("%f",&E.Salary);

printf("\n\tEnter Employee House No : ");


scanf("%s",&E.Add.HouseNo);

printf("\n\tEnter Employee City : ");


scanf("%s",&E.Add.City);

printf("\n\tEnter Employee House No : ");


scanf("%s",&E.Add.PinCode);

printf("\nDetails of Employees");
printf("\n\tEmployee Id : %d",E.Id);
printf("\n\tEmployee Name : %s",E.Name);

7
printf("\n\tEmployee Salary : %f",E.Salary);
printf("\n\tEmployee House No : %s",E.Add.HouseNo);
printf("\n\tEmployee City : %s",E.Add.City);
printf("\n\tEmployee House No : %s",E.Add.PinCode);

Output :

Enter Employee Id : 101


Enter Employee Name : Suresh
Enter Employee Salary : 45000
Enter Employee House No : 4598/D
Enter Employee City : Delhi
Enter Employee Pin Code : 110056

Details of Employees
Employee Id : 101
Employee Name : Suresh
Employee Salary : 45000
Employee House No : 4598/D
Employee City : Delhi
Employee Pin Code : 110056

Structure and functions


We can pass a structure as a function argument just like we pass any other variable or an array as
a function argument.

Example:

#include<stdio.h>

struct Student
{
char name[10];
int roll;
};

void show(struct Student st);

void main()

8
{
struct Student std;
printf("\nEnter Student record:\n");
printf("\nStudent name:\t");
scanf("%s", std.name);
printf("\nEnter Student rollno.:\t");
scanf("%d", &std.roll);
show(std);
}
void show(struct Student st)
{
printf("\nstudent name is %s", st.name);
printf("\nroll is %d", st.roll);
}

Unions
Unions are conceptually similar to structures. The syntax to declare/define a union is also similar
to that of a structure. The only differences is in terms of storage. In structure each member has its
own storage location, whereas all members of union uses a single shared memory location which
is equal to the size of its largest data member.

This implies that although a union may contain many members of different types, it cannot
handle all the members at the same time. A union is declared using the union keyword.
union item
{
int m;
float x;
char c;
}It1;

This declares a variable It1 of type union item. This union contains three members each with a
different data type. However only one of them can be used at a time. This is due to the fact that
only one location is allocated for all the union variables, irrespective of their size. The compiler
allocates the storage that is large enough to hold the largest variable type in the union.
In the union declared above the member x requires 4 bytes which is largest amongst the members
for a 16-bit machine. Other members of union will share the same memory address.
________________________________________
Accessing a Union Member in C
Syntax for accessing any union member is similar to accessing structure members,

9
union test
{
int a;
float b;
char c;
}t;

t.a; //to access members of union t


t.b;
t.c;

Example
#include <stdio.h>

union item
{
int a;
float b;
char ch;
};

int main( )
{
union item it;
it.a = 12;
it.b = 20.2;
it.ch = 'z';

printf("%d\n", it.a);
printf("%f\n", it.b);
printf("%c\n", it.ch);

return 0;
}
Out put
-26426
20.1999

10
Difference between Structure and Union in C

STRUCTURES UNION

Struct keyword is used to declare the


Union keyword is used to declare the Union
structure
Structure variable will allocate
Union variable will allocate common memory for all the
memory for all the structure
union members.
members separately.
Example: Example:
struct Employee{ union Employee{
int age; int age;

char name[50]; char name[50];


float salary; float salary;
}; };
Structures will occupy more memory
space.Memory_Size = addition of all
Union will occupy less memory space compared to
the structure members sizes.
structures.Memory_Size = Size of the largest Union
Memory_Size = int + char array [50]
member. From the above example, Largest Union member is
+ float
char array so, Memory_Size = 50 Bytes
Memory_Size = 2 + 50 + 4 Bytes
Memory_Size = 56 Byte
It allows us to access any or all the
It allows us to access only one union member at a time.
members at any time.

Enumeration data types in c


Enumeration is a user defined datatype in C language. It is used to assign names to the integral
constants which makes a program easy to read and maintain. The keyword “enum” is used to
declare an enumeration.

Here is the syntax of enum in C language,

enum enum_name{const1, const2, ....... };

The enum keyword is also used to define the variables of enum type. There are two ways to
define the variables of enum type as follows.
enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday};
enum week day;
Here is an example of enum in C language,

11
Example
#include<stdio.h>
{
enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun};
enum day{Mond, Tues, Wedn, Thurs, Frid=18, Satu=11, Sund};
int main() {
printf("The value of enum week: %d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",Mon , Tue, Wed, Thur,
Fri, Sat, Sun);
printf("The default value of enum day: %d\t%d\t%d\t%d\t%d\t%d\t%d",Mond , Tues, Wedn,
Thurs, Frid, Satu, Sund);
return 0;
}
Output
The value of enum week: 10 11 12 13 10 16 17
The default value of enum day: 0 1 2 3 18 11 12

In the above program, two enums are declared as week and day outside the main() function. In
the main() function, the values of enum elements are printed.
enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun};
enum day{Mond, Tues, Wedn, Thurs, Frid=18, Satu=11, Sund};
int main() {
printf("The value of enum week: %d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",Mon , Tue, Wed, Thur,
Fri, Sat, Sun);
printf("The default value of enum day: %d\t%d\t%d\t%d\t%d\t%d\t%d",Mond , Tues, Wedn,
Thurs, Frid, Satu, Sund);
}

12

You might also like