0% found this document useful (0 votes)
52 views8 pages

Example: Where : Prepared By: Dattu Kumar, Page - 1

The document discusses pointers, structures, and unions in C programming. It defines pointers as variables that store memory addresses and can be used to access data indirectly. Structures are used to define user-defined data types that contain heterogeneous data members. Structures allow defining complex data types by grouping related data. Unions are similar to structures but allocate memory for the largest data member only, allowing different members to share the same memory location. The document provides examples of declaring and using pointers, structures, and unions in C code. It also describes pointer arithmetic, nested structures, arrays of structures, and initializing structure variables.

Uploaded by

K Gangothri
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)
52 views8 pages

Example: Where : Prepared By: Dattu Kumar, Page - 1

The document discusses pointers, structures, and unions in C programming. It defines pointers as variables that store memory addresses and can be used to access data indirectly. Structures are used to define user-defined data types that contain heterogeneous data members. Structures allow defining complex data types by grouping related data. Unions are similar to structures but allocate memory for the largest data member only, allowing different members to share the same memory location. The document provides examples of declaring and using pointers, structures, and unions in C code. It also describes pointer arithmetic, nested structures, arrays of structures, and initializing structure variables.

Uploaded by

K Gangothri
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/ 8

B.Com(CA) - II Semester *** SRK Degree & P.G.

College - Kamareddy *** PWC&C++ UNIT-IV


UNIT-IV: POINTERS, STRUCTURES AND UNIONS
Pointers: Features of pointers- Declaration of Pointers-arithmetic operations with pointers.
Structures: Features of Structures - Declaring and initialization of Structures –Structure within Structure-
Array of Structures- Enumerated data type.
Unions: Definition and advantages of Unions comparison between Structure & Unions.
----------------------------------------------------------------------------------------------------------------------------------
Pointers
Def 1: A pointer is a variable which holds the address of another variable or identifier.
Def 2: A pointer is used to store address of another variable.
 A pointer variable cannot be multiplied or divided by a constant.

:: Features (Advantages) of Pointers: ::


 Pointer allows us to access the data indirectly.
 A pointer variable can be assigned a NULL value.
 C allows us to add integers to or subtract integers from pointers.
 Pointer variable can be incremented or decremented.

Disadvantages:
 Using pointers sometimes be confusions.
 A pointer variable should be define & initialized properly. Otherwise it leads to disastrous
situations.

Address of a variable:
 In C language, the address of a variable ‘a’ is defined as: ‘&a’.
 Here, the ampersand(&) symbol is called as reference (address) operator.
 We have to assign this ‘&a’ (address of ‘a’) to the pointer variable as reference.

Example: ptr = &a; Where; ptr – pointer variable


&a – address of variable ‘a’.

:: Declaration & initialization o a pointers ::


Syntax: Data type *pointer_variable ; // Pointer Declaration
pointer_variable = &variable; // Pointer Initialization with address of variable

Example: int *p,i; // Declaration of pointer and variable (integers)


p=&i; //Initialization.
The declaration tells the compiler 3 things about variable p
a) The asterisk (*) tells that variable p is a pointer variable
b) p needs a memory location
c) p points to a variable of same data type.

Prepared By: Dattu Kumar, Lecturer in Computer Science Page| 1


B.Com(CA) - II Semester *** SRK Degree & P.G. College - Kamareddy *** PWC&C++ UNIT-IV
//Program to explain Pointer
#include<stdio.h>
main ( )
{ int x = 10;
int *ptr;
ptr=&x;
printf ("x value is: %d \n",x);
printf ("ptr value is: %d",*ptr);
getch();
}
:: Arithmetic Operations with pointers ::
 The pointer contains address.
 When we perform arithmetic operations on pointers, the result also will be a pointer.
 Pointers support the following 4 arithmetic operations:

1) Incrementing Pointer (++)


2) Decrementing Pointer (--)
3) Pointer addition (+)
4) Pointer Subtraction (-)
a) Incrementing Pointer (++):
When a pointer variable is incremented by 1, it points to the next location in the memory.
Syntax: int *ptr;
ptr = &variable;
ptr++;

//Program to explain Pointer Increment


#include<stdio.h>
main()
{ int num=50;
int *p;
p = &num; printf("Address of p variable is: %u \n", p); // %u Unsigned integer
p++; printf("After increment, Address of p variable is: %u \n", p);
getch();
}

b) Decrementing Pointer (--):


When a pointer variable is decremented by 1, it points to the previous location in the memory.
Syntax: int *ptr;
ptr = &variable;
ptr--;

//Program to explain Pointer Decrement


#include<stdio.h>
main()
{ int num=50;
int *p;
p = &num; printf("Address of p variable is: %u \n", p);
p--; printf("After decrement, Address of p variable is: %u \n", p);
getch();
}

Prepared By: Dattu Kumar, Lecturer in Computer Science Page| 2


B.Com(CA) - II Semester *** SRK Degree & P.G. College - Kamareddy *** PWC&C++ UNIT-IV
c) Pointer Addition (+):
We can add a value to the pointer variable to change its location.
Syntax: int *ptr;
ptr = &variable;
ptr = ptr + 3;

//Program to explain Pointer Addition


#include<stdio.h>
main()
{ int num=50;
int *p;
p = &num; printf("Address of p variable is: %u \n", p);
p=p+3; printf("After adding 3, Address of p variable is: %u \n", p);
getch();
}

c) Pointer Addition (+):


We can subtract a value from the pointer variable to change its location.
Syntax: int *ptr;
ptr = &variable;
ptr = ptr - 3;

//Program to explain Pointer Addition


#include<stdio.h>
main()
{ int num=50;
int *p;
p = &num; printf("Address of p variable is: %u \n", p);
p=p-3; printf("After subtracting 3, Address of p variable is: %u \n", p);
getch();
}
*****

Prepared By: Dattu Kumar, Lecturer in Computer Science Page| 3


B.Com(CA) - II Semester *** SRK Degree & P.G. College - Kamareddy *** PWC&C++ UNIT-IV
STRUCTURES
Def 1: Structure is a collection of heterogeneous (different) type of data that share a common name.
Def 2: A structure is a method of packing a data of different types.
:: Features (Advantages) of Structures ::
 Structures are used to store different values of different data types.
 Mainly used to create data bases.
 Nested structures: A structure can be defined in another structure.
 Array of Structures: different structures can defines as an array.
 A structure variable can be passed as an argument to a function.

:: Declaring & Initialization of Structures ::


Declaration of Structures:
 A structure can be declared by using the keyword ‘struct’
 A ‘tag name’ is given to the structure as structure name.
 Then define the variables of different data types as ‘data members’ of the structure.
 Finally we have to define a ‘structure variable’ to access the data members of the structure.

Syntax: struct structure_name


{ data_type variable_1;
data_type variable_2;
data members
:::::::::::::::::::::::::::::::::::::::
data_type variable_n;
} structure_variable;
Example: struct bcom
{ int a,b,c;
float f1,f2[10]; data members
char ch=”srk”;
}k; (where: bcom- tag name, k- structure variable)

Initialization of Structures:
We can initialize (or) access the data members of a structure through structure variable using a
dot(.) operator.
Syntax: structure-variable.data_member = value;
Example: k.a=10;
printf(“a value is: %d”,k.a);

Prepared By: Dattu Kumar, Lecturer in Computer Science Page| 4


B.Com(CA) - II Semester *** SRK Degree & P.G. College - Kamareddy *** PWC&C++ UNIT-IV
// Program to read & write a structure
#include<stdio.h>
main()
{ struct student
{ int rno;
char name[20];
}k;
printf("Enter Roll No, Name of the student \n");
scanf("%d%s", &k.rno, &k.name);
printf("Roll no: %d \n Name: %s ",k.rno,k.name);
getch();
}

//Program to calculate average marks of the student using structures


#include<stdio.h>
main()
{ struct student
{ char name[20];
int s1,s2,s3,s4,s5,s6,total;
float avg;
}k;
printf(“Enter Student Name, Marks of s1,s2,s3,s4,s5,s6 \n”);
scanf(“%s%d%d%d%d%d%d”,&k.name, &k.s1, &k.s2, &k.s3, &k.s4, &k.s5, &k.s6);
k.total= k.s1+k.s2+k.s3+k.s4+k.s5+k.s6;
k.avg=k.total/6;
printf(“Name: %s \n Total Marks: %d \n Average: %f”,k.name,k.total,k.avg);
getch();
}
Structure within Structure (Nested Structures)
 A structure which includes another structure is called nested structure
 i.e a structure can be used as a member of another structure.

Syntax: struct structure_name


{data_type variable_1;
data_type variable_2;
struct structure_name
{ data_type variable_1;
data_type variable_2;
::::::::::::::::::::::::::::::::::::::::::
data_type variable_n;
} structure_variable;
data_type variable_n;
} structure_variable;

Prepared By: Dattu Kumar, Lecturer in Computer Science Page| 5


B.Com(CA) - II Semester *** SRK Degree & P.G. College - Kamareddy *** PWC&C++ UNIT-IV
//Sample program on structures
#include<stdio.h>
main()
{ struct student
{ int rno;
char name[20];
struct date
{ int day;
char month[10];
int year;
}dob;
}k;
printf("Enter Roll No, Name of the student \n");
scanf("%d%s", &k.rno, &k.name);
printf(“Enter Date of Birth of the student:\n”);
scanf("%d%s%d", &k.dob.day, &k.dob.month, &k.dob.year);

printf("Roll no: %d \n Name: %s \n",k.rno,k.name);


printf(“Date of Birth: %d-%s-%d ”, k.dob.day, k.dob.month, k.dob.year);
getch();
}
Functions and Structures:
We can use the structures with functions to pass the arguments and return values also.
i.e; we can call a function by passing the structure members as actual variables.
//Program to use Structures with Functions.
#include<stdio.h>
main( )
{ struct s
{ int a,b;
}k={10,5};
printf("Sum of two numbers :%d",add(k.a,k.b ));
getch( );
}
add(int x,int y)
{ return (x+y); }
:: Arrays of Structures ::
A Structure is collection of different data types (variables ) which are grouped together.
Whereas, array of structures is nothing but collection of structures.
Syntax: struct structure_name
{ data_type variable_1;
data_type variable_2;
data members
:::::::::::::::::::::::::::::::::::::::
data_type variable_n;
} structure_array_variable;

Prepared By: Dattu Kumar, Lecturer in Computer Science Page| 6


B.Com(CA) - II Semester *** SRK Degree & P.G. College - Kamareddy *** PWC&C++ UNIT-IV

Example: struct bcom


{ int a,b,c;
float f1,f2[10]; data members
char ch=”srk”;
}k[5];

//Program to explain Array of structures


#include<stdio.h>
#include<string.h>
struct student
{ int rollno[20];
char name[20];
}st[5];
main()
{ int i;
printf("Enter Records of 5 students");
for(i=0;i<5;i++) { printf("\n\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("Student Information List:\n");
for(i=0;i<5;i++)
{ printf("\t Rollno:%d \t Name:%s \n", st[i].rollno, st[i].name); }
getch();
}
:: Enumerated Data Type ::
 An enumeration is a user-defined data type that consists of integral constants.
 To define an enumerate data type we have to use the keyword ‘enum’.
Syntax: enum flag { const1, const2, ..., constN };
Example: enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday };
//Program to explain Enumerated Datatype(week)
#include<stdio.h>
main()
{ enum week{Sun, Mon, Tue, Wed, Thur, Fri, Sat}day; //enum week day;
day = Wed;
printf("%d",day+1);
getch();
}
//Program to explain Enumerated Datatype(month)
#include<stdio.h>
main()
{ enum month{jan,feb,mar,apr,may,june,july,aug,sep,oct,nov,dec}month;
month = oct;
printf("%d",month+1);
getch();
}

Prepared By: Dattu Kumar, Lecturer in Computer Science Page| 7


B.Com(CA) - II Semester *** SRK Degree & P.G. College - Kamareddy *** PWC&C++ UNIT-IV
UNIONS
 A union is one of the derived data types.
 Union is a collection of variables referred under a single name.
 Unions are used to store only one type of data at a time.
The syntax, declaration & use of union are similar to the structure but its functionality is different.
:: Advantages of Unions ::
 Unions are used to store different values of different data types.
 Mainly used to create data bases.
 Less & efficient use of memory.
 Does not demand memory for all data members.

:: Declaring a Union ::
 A union can be declared by using the keyword ‘union’
 A ‘tag name’ is given to the union as union name.
 Then define the variables of different data types as ‘data members’ of the union.
 Finally we have to define a ‘union variable’ to access the data members of the union.

Syntax: union union _name


{ data_type variable_1;
data_type variable_2;
data members
:::::::::::::::::::::::::::::::::::::::
data_type variable_n;
} union_variable;

Example: union bcom


{ int a,b,c;
float f1,f2[10]; data members
char ch=”srk”;
}k; (where: bcom- tag name, k- union variable)

:: Comparison between Structures & Unions ::


Structures Unions
1. A structure is a method for packing a data of 1. A union is same as structure. But it can handle
different types. only one data member at a time.

2. We use ‘struct’ keyword to define a structure. 2. . We use ‘union’ keyword to define a union.

3. Structures are widely used. 3. Unions are rarely used.

*****

Prepared By: Dattu Kumar, Lecturer in Computer Science Page| 8

You might also like