You are on page 1of 8

Pointer, Structure and Union

1
Explain Concept of pointers and advantages of pointer:Pointers are widely used in programming; they are used to refer to memory location of
another variable without identifier itself.
Pointers are mainly used linked list and call by references functions.
Every pointer is also variable.
The pointer contains address of another variable to which the pointer pointing.
To point a variable, the pointer must be of same data type as the variable.
To declare pointer we use * operator.
& notation means address of operand in this case &a means address of a
Example
main()
{
int i=10,*ptr;
ptr=&i;
printf(\n\n The value of i %d, i);
printf(\n\n The value of i through pointer %d, *ptr);
printf(\n\n The address of i %d, &i);
printf(\n\n The address of pointer %d, ptr);
getch();
}
Advantages of pointer:Pointer can be used to return multiple values from function via arguments or
parameters.
Pointer is easy use for handling array.
Pointers array using to character string results in saving of data storage space in
memory.
Pointers reduce length & complexity of program.
More compact and efficient coding.
It supports dynamic memory allocation.
C uses pointers explicitly with Array, function & structure.
Arithmetic of Pointer:Arithmetical operations are possible with pointers. Pointers can be added and subtracted.
However pointer arithmetic is quite meaningless performed in arrays. Addition and
subtraction are mainly for moving forward and backward array.
Operator
++
--= or += or +

Result
Goes to the next memory location the pointer is pointing to.
Goes to the pervious memory location the pointer is pointing to.
Subtract value of pointer
Adding to the pointer

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T) 9974846443(M)


D.C.Shah BCA College , Mandvi

Pointer, Structure and Union

Example
main()
{
int a[3]={4,5,6};
int *ptr;
ptr=a;
printf(address %d array value %d\n,ptr,*ptr);
ptr++;
printf(address %d array value %d\n,ptr,*ptr);
}
Array of pointers
We can declare an array as a pointer. Every element of this array can hold address of any
variable. We can say that every element of this array is a pointer variable. It is same as
array but it is a collection of addresses.
void main()
{
int *a[3];
int x = 5,y = 10, z = 15,i;
clrscr();
a[0] = &x;
a[1] = &y;
a[2] = &z;
for(i=0; i<3; i++)
{
printf("address = %u\t", a[i]);
printf("value = %d\n", *(a[i]));
}
getch();
}
In above example a is declared as a array of pointer. Every element of this array holds the
address of the variable. a[i] gives the address of the ith element of a and *a[i] gives the
value at this address.
Ex: - To Display the array using element using pointer
main()
{
int i,n,*pa;
printf("Input the value of N=>");
scanf("%d",&n);
pa=malloc(n);
for(i=0;i<n;i++)
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T) 9974846443(M)
D.C.Shah BCA College , Mandvi

Pointer, Structure and Union

{
scanf("%d",(pa+i));
}
printf("\n print using dynamic array\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(pa+i));
}
getch();
}
POINTER AND FUNCTIONS
The arguments or parameters to the functions are passed in two ways.
1. Call by value
2. Call by reference
In call by value the values of the variables are passed. In this value of variable are not
affected by changing the value of the formal parameter.
void main()
{
int a = 5;
int b = 8;
clrscr();
printf("Before Calling the function a and b are %d and %d \n", a,b);
value(a,b);
printf("After Calling the function a and b are %d and %d \n", a,b);
getch();
}
value(int p, int q)
{
p++;
q++;
printf("In the function p and q are %d and %d \n", p,q);
}
In call by reference the address of the variable are passed. In this value of variables are
affected by changing the value of the formal parameter.
void main()
{
int a = 5;
int b = 8;
clrscr();
printf("Before Calling the function a and b are %d and %d \n", a,b);
value(&a,&b);
printf("After Calling the function a and b are %d and %d
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T) 9974846443(M)
D.C.Shah BCA College , Mandvi

Pointer, Structure and Union

\n", a,b);
getch();
}
value(int *p, int *q)
{
(*p)++;
(*q)++;
printf("In the function p and q are %d and %d \n", *p,*q);
}
/* Result of execution
Before Calling the function a and b are 5 and 8
In the function p and q are 6 and 9
After Calling the function a and b are 6 and 9
*/
In above example the address of a and b are passed to p and q which is a pointer
variable. (*p)++ means value at address is incremented. Similarly (*q)++ means value at
address 2000, which is 8 incremented by 1.
Now, the value of *p = 6 and *q = 9.
Here the address is not changed but value at this address is changed. Therefore after
calling the function the value of variables and b are changed.
What is Structure? Explain with example
Structure is a user defined data type which is use to store collection of element with
different data type.
Structure is use to encapsulate, or group together different data type.
Syntax
struct <struct_name>
{
Datatype mem1;
Datatype mem2;

}var_name;
Example
main()
{
struct student
{
int rn;
char name[20];
char mf;
int age;
};
struct student s;
clrscr();
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T) 9974846443(M)
D.C.Shah BCA College , Mandvi

Pointer, Structure and Union

s.rn=11;
strcpy(s.name,"vimal");
s.mf='V';
s.age=26;
printf("\n Roll No:%d",s.rn);
printf("\n Name:%s",s.name);
printf("\n Male Female:%c",s.mf);
printf("\n Age: %d",s.age);
getch();
}
How to take input from user with Structure
main()
{
struct student
{
int rn;
char name[20];
char mf;
int age;
}s[3];
int i;
clrscr();
for(i=0;i<2;i++)
{
printf("Enter roll no:=>");
scanf("%d",&s[i].rn);
printf("Enter Name:=>");
scanf("%s",s[i].name);
printf("Enter Gender:=>");
scanf("%s",&s[i].mf);
printf("Enter age:=>");
scanf("%d",&s[i].age);
}
for(i=0;i<2;i++)
{
printf("\n %d \t %s \t %c \t %d",s[i].rn,s[i].name,s[i].mf,s[i].age);
}
getch();
}
Explain Nested Structure with Example.
A structure may have structure as members called nested Structure. It means if a
structure having structure is called nested structure.
main()
{
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T) 9974846443(M)
D.C.Shah BCA College , Mandvi

Pointer, Structure and Union

struct date
{
int d;
int m;
int y;
};
struct
{
int rn;
char name[20];
struct date dob;
}s={1,"vimal",20,1,1986};
printf("\nRoll no:=>%d",s.rn);
printf("\nName:=>%s",s.name);
printf("\n Brithdate:=>%d %d %d",s.dob.d,s.dob.m,s.dob.y);
getch();
}
Use of structures with function
Structures are user defined data types; function can return structures and also take them
as arguments.
To return a structure from a function declares the function to be of the structure type
which is suppose to return.
main()
{
struct student
{
int rn;
char name[20];
}s={10,"vimal"};
void test(int , char[]);
clrscr();
test(s.rn,s.name);
getch();
}
void test(int x,char y[])
{
printf("\n Roll no:=>%d",x);
printf("\n Name:=>%s",y);
}
To pass entire Structure
struct student
{
int rn;
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T) 9974846443(M)
D.C.Shah BCA College , Mandvi

Pointer, Structure and Union


char name[20];
};
main()
{
void test(struct student);
struct student s={10,"vimal"};
clrscr();
test(s);
getch();

}
void test(struct student s)
{
printf("\n Roll no:=>%d",s.rn);
printf("\n Name:=>%s",s.name);
}
Structure with pointer
main()
{
struct student
{
int rn;
char name[20];
char gender;
}s={1,"VIMAL",'M'},*p=&s;
clrscr();
printf("\n Roll no:=>%d",p->rn);
printf("\n Name:=>%s",(*p).name);
printf("\n Gender:=>%c",p->gender);
getch();
}
Explain Union with example
Unions and Structure are identical in almost all ways, except for one very important
aspect.
Only one element in union may have a value set any given time.
Unions are mainly use to conserve memory. While each member within a structure is
assigned its own unique storage area, the members that compose a union share the
common storage area within the memory.
Unions are useful for application involving multiple members where values are not
assigned to all members at one time.
main()
{
union student
{
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T) 9974846443(M)
D.C.Shah BCA College , Mandvi

Pointer, Structure and Union

float l;
char *p;
};
union student s;
clrscr();
s.l=15.50;
s.p="This is Union";
printf("\n %.2f",s.l);
printf("\n %s",s.p);
getch();
}

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T) 9974846443(M)


D.C.Shah BCA College , Mandvi

You might also like