0% found this document useful (0 votes)
8 views21 pages

Prog. With C & C++ (Unit-IV)

The document provides an introduction to pointers and structures in C programming, detailing how pointers can hold memory addresses and how to declare and initialize them. It also explains the features of structures, including their ability to store multiple data types, and how to declare, initialize, and access structure members. Additionally, it covers arrays of structures and enumerated data types, emphasizing their utility in managing complex data efficiently.

Uploaded by

konad47993
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)
8 views21 pages

Prog. With C & C++ (Unit-IV)

The document provides an introduction to pointers and structures in C programming, detailing how pointers can hold memory addresses and how to declare and initialize them. It also explains the features of structures, including their ability to store multiple data types, and how to declare, initialize, and access structure members. Additionally, it covers arrays of structures and enumerated data types, emphasizing their utility in managing complex data efficiently.

Uploaded by

konad47993
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/ 21

Page No: 1

1. Pointers
Introduction to Pointers:
Pointer is also a variable which can hold the address of another variable (address
of a normal variable).
While developing any program in C, whenever we declare a variable then the
sufficient memory will be allocated for that variable and also by default the syatem will
provide a specific address for that memory location.
Example: 2 bytes size of variable a
int a; a 10 value of a
a=10; 1002 address of a

Note: In C programming language we can access the values which are stored in the
memory locations in two ways. They are;
1. By using variable name (Direct access)
2. By using address of variable (Indirect access)
In indirect access we use the concept called ponters.

Address of operator ( & ):


Address of operator is denoted by the symbol ampersand (&). And it is used to
access the address of the variable.
Example: 2 bytes size of variable a
int a, n; a 10 value of a
a=10; 1002 address of a
n=a; it is valid statement
n=&a; it is not a valid statement.

In the above example, variable n cannot store the address of a. Because n is a normal
variable. To store the address of any variable we require a pointer variable.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 2

Declaration of Pointers:

Declaration of pointers is nothing but defining its name and type to allocate the
memory space for the pointers.

Syntax: datatype *ptr ;

Or

datatype *ptr1, *ptr2, *ptr3,… ;

In the above syntax,


datatype may be any fundamental datatype.
ptr1, ptr2, ptr3… are pointer variable.
* (asterisk) symbol is used to declare pointer variable. And it should be
used before the variable name.
Example:

int *p1; p1 is integer pointer

float *p2; p2 is floating pointer

char *p3; p3 is character pointer

Initialization of Pointers:

Assigning the address of a variable to the pointer is called as initialization of


pointer.

Syntax: datatype *ptr = &variablename ;

In the above syntax;

datatype may be any fundamental type.

ptr is a pointer variable.

* (asterisk) symbol to declare pointer.

& (ampersand) symbol to access the address of the variable.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 3

Example: 2 bytes size of variable a


int a; a 10 value of a
a=10; 1002 address of a
int *p;
p=&a; p 1002

Note 1: A normal variable must be declared before the declaration of pointer variable.
Example:
int *p;
p=&a;
int a;
a=10;
The above statements are not valid statement. Because a normal variable a is
declared after pointer variable declaration.

Note 2: Normal variable type and pointer variable type must be same.
Example:
float a;
a=10.50;
int *p;
p=&a;
The above statements are not valid. Because normal variable type is float and
pointer variable type is int.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 4

Indirection operator ( * ):
In C programming language we can develop any program in two ways. They are;
1. Without pointers (Direct access)
2. By using pointers (Indirect access)
Indirection operator is used to access the values through the pointers. This
indirection operator should be written before the pointer variable.
Example:
int a;

a=10; a 10
int *p; 1002

p=&a; p 1002

Direct access: printf(“The value of a = %d”, a);


Indirect access: printf(“The value of a = %d”, *p);
Whenever we write the indirection operator * before the pointer variable, then it
gives the value at the address which is stored in the pointer variable.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 5

Arithmetic operations with pointers:


We can perform various arithmetic operations like addition, subtraction,
multiplication, etc. by using pointers. Those operations are discussed below by taking
the example program.
Ex-1: Write a program to perform addition operation using indirection operator (pointer expression).

` #include<stdio.h>
void main()
{
int a, b, c;
a=10;
b=20;
int *p1, *p2;
p1=&a;
p2=&b;
c=*p1 + *p2; // pointer expression
printf(“The addition value = %d”, c);
}

Output:
The addition value = 30

Ex-2: Write a program to perform subtraction operation using indirection operator (pointer
expression).

#include<stdio.h>
void main()
{
int a, b, c;
a=10;
b=20;
int *p1, *p2;
p1=&a;
p2=&b;
c=*p1 - *p2; // pointer expression
printf(“The subtraction value = %d”, c);
}

Output:
The subtraction value = -10

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 6

Features of Pointers:
Pointers have many features. Some they are discussed below;

 Pointers save memory space.


 Execution time with pointers is faster because data are manipulated with the
address, that is, direct access to memory location.
 Memory is accessed efficiently with the pointers. The pointer assigns and releases
the memory as well. Hence it can be said the Memory of pointers is dynamically
allocated.
 Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional arrays.
 An array, of any type can be accessed with the help of pointers, without
considering its subscript range.
 Pointers are used for file handling.
 Pointers are used to allocate memory dynamically.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 7

2. Structures
Features of Structures:
Structure is an example for user-defined data type where we can store multiple
values of different types.

The structures in C programming language have the following features;

1) Using the “=” operator:

Structure elements are stored in the memory in contiguous memory locations


which allow us the use of “=” (the equals to assignment operator) to copy the entire
elements of one structure to another. This is not possible in case of arrays where we
need to copy all the elements one by one in order to copy the entire elements.

Here is an example of how we can simply equate two structures to copy the
values from one to another.

struct student s1 = {"Ramu", 101,79.0};

struct student s2 = s1; // copies all the elements of s1 to s2

2) Nested Structures:

C programming allows us to create nested structures, i.e., one structure within


another structure. This property enables us to create complex data types for storing
various elements. The nesting process can continue as long as the user wants.

3) Passing Structure Variables to a Function:

This feature of structure allows us to pass any of the structure variables to the
function like any other normal variable. This can be done in two ways: Passing
individual structure elements to the function or passing the entire structure to the
function.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 8

Declaring a structure:

Syntax:

struct <Structurename>
{
member1;
member2;
member3;
.
.
.
membern;
};

In the above syntax;

 “struct” is a keyword used to declare structures.


 Structurename represents name of the structure. It is also called tag name.
 member1, member2, membern,… are the members of structure. These members
are also called characteristics of structure. Each member should be terminated by
semicolon.
 Finally the structure must be terminated by semicolon.

Example: struct account


{
int accno;
char name[];
float balance;
};
Note: Whenever we declare any structure then no memory will be allocated for
structure members. To allocate memory for all its structure members, we should
declare the variable for that structure.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 9

Declaring structure variable:

Declaring a structure variable is nothing but allocating sufficient memory for


structure members.

Syntax-1: struct Structurename variablename ;

Syntax-2: struct Structurename variable1, variable2, variable3,… ;

Example: struct account


{
Int accno;
Char name[];
Float balance;
};
struct account person1, person2, person3;

Whenever the statements are executed then internally memory will be allocated
for all the structure members with respective structure variables as below;

person1 person2 person3

accno

name

balance

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 10

Initializing a structure:

Initialization of structure is nothing but storing the values in to the structure


members.

Syntax-1: struct Structurename variablename = ,value1, value2, value3,…-;

Syntax-2: struct Structurename v1, v2, v3,… = , ,value1, value2, value3,…-,


,value1, value2, value3,…-,
{value 1, value2, value3,…-
.
.
.
};
Example:
struct account
{
int accno;
char name[];
float balance;
};
Struct account person1, person2, person3 = , , 1001, “Jyothi”, 25000.00,
, 1002, “Krish”, 28000.00,
, 1003, “Venkey”, 30000.00- -;
Whenever the above statements are executed; internally all the values will be
stored into the memory locations as below;

person1 person2 person3

accno 1001 1002 1003


name Jyothi Krish Venkey
balance
25000.00 28000.00 30000.00

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 11

Accessing members of Structure:

As we know the value of a normal variable can be accessed directly by its name.
To access the elements of an array we use its indexes. In the same way to access the
members of structures we can use member operator (dot operator).It is denoted by the
symbol dot ( . ).

Syntax: variablename . membername ;

Example-1: person1 . name ;

The above statement gives the name of the person1 variable.

Example-2: person2 . balance ;

The above statement gives the balance of the perso2 variable.

Example: A program which explains the concept of structures.

#include<stdio.h>
#include<conio.h>
void main()
{
struct account
{
int accno;
char name[];
float balance;
};
struct account person1, person2 = , ,1001, “Jyothi”, 25000.00-, ,1002, “Krish”, 28000.00- -;
printf(“ Person-1 Details: ”);
printf(“ \n Account Number = %d “, person1.accno);
printf(“\n Name = %s “, person1.name);
printf(“ \n Balance = %f “, person1.balance);
printf(“ \n Person-2 Details: ”);
printf(“ \n Account Number = %d “, person2.accno);
printf(“ \n Name = %s “, person2.name);
printf(“ \n Balance = %f “, person2.balance);
}

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 12

Output:
Person-1 Details:
Account Number = 1001
Name = Jyothi
Balance = 25000.00
Person-2 Details
Account Number = 1002
Name = Krish
Balance = 28000.00

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 13

Arrays of structures:

In the previous topics we discussed about declaration of structures and


declaration of structure variables. That is to allocate memory for structure members we
must declare structure variable.
Whenever we required to declare many variables for a structure, then we may
face some difficulties to identify all the variable names. In those cases we can declare
only one variable which is nothing but array variable to allocate many memory locations
(with respective individual variables). We specify the size for the array according to
required no.of variables.
In the previous example we declared only three variables person1, person2,
person3 as below;

struct account person1, person2, person3;

If there are 50 persons (variables) then no need to declare all the 50 variable
names. Simply we can declare one array with size 50 as below;

struct account person[50];

Example: struct account


{
int accno;
char name[];
float balance;
};
struct account person[50]; // array of structure

Whenever the above statement is executed, internally memory will be allocated


for 50 variables with respective structure members as below;

person[0] person[1] person*2+ ……….person*49+


accno …..
name …..
balance …..

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 14

After declaring an array variable, we can store the values into the array as below;

struct account person*50+ = , ,1001, “Jyothi”, 25000.00-,


,1002, “Krish”, 28000.00},
,1003, “Venkey”, 30000.00-,
…………..
,1049, “Anvika”, 50000.00- -;

Whenever the above statement is executed, all the values will be stored into the
memory locations as beloq;

person[0] person[1] person*2+ ……….person[49]


accno 1001 1002 1003 ….. 1049
name Jyothi Krish Venkey ….. Anvika
balance 25000.00 28000.00 30000.00 ….. 50000.00

To access the values of the array of structure we can use their indexes along with
member name as below;

Example-1: person[0]. name; // it gives name of the 1st person


Example-2: person[2].accno; // it gives account number of the 3rd person

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 15

Example: A program which explains the concept of arrays of structure.

#include<stdio.h>
#include<conio.h>
void main()
{
int I;
clrscr();
struct account
{
int accno;
char namr[];
float balance;
};
struct account person[5];
for(i=0;i<5;i++)
{
printf(“Enter Person %d Details: “, i+1);
scanf(“%d%s%f”, &person*i+.accno, person*i+.name, &person*i+.balance);
}
for(i=0;i<5;i++)
{
printf(“Person %d Details: “, i+1);
printf(“\n Account Number: “, person*i+.accno);
printf(“\n Name: “, person*i+.name);
printf(“\n Balance: “, person*i+.balance);
}
}

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 16

Enumerated data types:

Enumeration is also an example for user defined data type same as structures and
unions. Simply these are also called enums.

Declaration of Enumeration types:

Syntax: enum <Enumerationname>


{
member1,
member2,
member3,
.
.
.
membern
};
In the above syntax;
 enum is a keyword.
 Enumerationname represents name of the enumeration name (tag name).
 Member1, member2, member3,… are members of enumeration.
 Whenever the members are declared, each member will be assigned by integer
identifiers.
 The 1st member is assigned with 0 (zero) by default and sub-sequent members will
be assigned integers which are increased by 1.

Note: Programmers can also assign the integers values to the members.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 17

Example: A program which explains the concept of Enumeration types.

#include<stdio.h>
void main()
{
enum month
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
enum month a, b;
a=march;
b=August;
printf(“The value of March = %d”, a);
printf(“The value of August =%d”, b);
}

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 18

3. Unions:
Introduction to Unions:

Union is also an example for user-defined data type where we can store multiple
values of different types.

Unions have the following advantages;


 It occupies less memory compared to structure.
 When you use union, only the last variable can be directly accessed.
 Union is used when you have to use the same memory location for two or more
data members.
 It enables you to hold data of only one data member.
 Its allocated space is equal to maximum size of the data member.

Declaring a union:

Syntax:

union <Unionname>
{
member1;
member2;
member3;
.
.
.
membern;
};

In the above syntax;

 “union” is a keyword used to declare union.


 Unionname represents name of the Union. It is also called tag name.

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 19

 member1, member2, membern,… are the members of structure. These members


are also called characteristics of structure. Each member should be terminated by
semicolon.
 Finally, union must be terminated by semicolon.

Example: union account


{
int accno;
char name[];
float balance;
};
Note: Whenever we declare any union then no memory will be allocated for union
members. To allocate memory for all its union members, we should declare the variable
for that union.

Declaring union variable:

Declaring a union variable is nothing but allocating sufficient memory for union
members.

Syntax-1: union Unionname variablename ;

Syntax-2: union Unionname variable1, variable2, variable3,… ;

Example: union account


{
int accno;
char name[];
float balance;
};
struct account person;

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 20

Whenever the statements are executed then internally memory will be allocated
for all the union members with respective union variable as below;

person

accno

name

balance

Note-1: If we declare a structure variable then the memory will be created for each and
every structure member separately. Whereas, if we declare the variable for union then
only one memory location is allocated for all the union members.
Note-2: Union members are also accessed by using dot operator same as structure
members.

Example: A program which explains the concept of unions.

#include<stdio.h>
void main()
{
union account
{
int accno;
char name[];
float balance;
};
union account person;
printf(“Enter Person Details: “);
scanf(“%d%s%f”, &person.accno, person.name, &person.balance);
printf(“ \n Person Details: ”);
printf(“ \n Account Number = %d “, person.accno);
printf(“\n Name = %s “, person.name);
printf(“ \n Balance = %f “, person.balance);
}

B.Com (Comp. Applications) I Year, SEMESTER-II


Page No: 21

Comparison between Structures and Unions:

Structures Unions
Structure is an example for User-defined Union is also an example for User-
data type. defined data type.
The keyword struct is used to declare The keyword union is used to declare the
the structures. unions.
Syntax: Syntax:
struct <structurename> union <unionname>
{ {
Member1; Member1;
Member2; Member2;
….. …..
}; };

In the structures, memory will be In the unions, memory will be allocated


allocated for the structure members for the union members when we declare
when we declare structure variable. union variable.
Memory will be allocated for all the In the unions, only one memory location
members separately (individually). will be allocated for all the members.
Members can be accessed by using dot Here also, members will be accessed by
operator. using dot operator.
All the values will be available entire the Here only one value will be available
program. which is entered at last.

B.Com (Comp. Applications) I Year, SEMESTER-II

You might also like