Documente Academic
Documente Profesional
Documente Cultură
Topics Covered
1. Pointers
2. Structures
3. Unions
1.0 Introduction to Pointers
Pointers are variables that hold address of another variable of same data type. It is also known
as locator or indicator that points to an address of a value.
Advantage of pointer
Name
Description
address of operator
* (asterisk sign)
indirection operator
Concept of Pointer
Whenever a variable is declared, system will allocate a location to that variable in the memory,
to hold value. This location will have its own address number. Let us assume that system has
allocated memory location fff4 for a variable number
int number = 50 ;
We can access the value 50 by either using the variable name number or the address fff4. Since
the memory addresses are simply numbers they can be assigned to some other variable. The
variable that holds memory address is called pointer variables. Simply a pointer variable holds
an address, which is a location of another variable. Value of that pointer variable will be stored
in another memory location.
Pointer example
As you can see in the figure, pointer variable
stores the address of number variable i.e.
fff4. The value of number variable is 50. But
the address of pointer variable p is aaa3.
&
(immediately preceding a variable name) returns the address of the variable associated with it.
Dereferencing of Pointer
Once a pointer has been assigned the address of a variable. To access the value of variable,
pointer is dereferenced, using the indirection operator *.
NULL Pointer
A pointer that is not assigned any value but NULL is known as NULL pointer. If you don't have
any address to be specified in the pointer at the time of declaration, you can assign NULL value.
It will a better approach.
int *p=NULL;
Note: In most the libraries, the value of pointer is 0 (zero).
C Pointer to Pointer
In C pointer to pointer means, a pointer refers to the address of another pointer which points to
the address of a value. Let's understand it by the diagram given below:
As you can see in the above figure, p2 contains the address of p (fff2) and p contains the
address of number variable (fff4).
#include <stdio.h>
#include <conio.h>
void main()
{
int number=50;
int *p;//pointer to int
int **p2;//pointer to pointer
clrscr();
p=&number;//stores the address of number variable
p2=&p;
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of *p variable is %d \n",*p);
printf("Address of p2 variable is %x \n",p2);
printf("Value of **p2 variable is %d \n",**p);
getch();
}
Output
Address of number variable is fff4
Address of p variable is fff4
Value of *p variable is 50
Address of p2 variable is fff2
Value of **p variable is 50
Pointer Arithmetic in C
In C pointer holds address of a value, so there can be arithmetic operations on the pointer
variable. Following arithmetic operations are possible on pointer in C language:
Addition
Subtraction
C Pointer Addition
We can add a value to the pointer variable. The formula of adding value to pointer is given
below:
new_address= current_address + (number * size_of(data type))
Let's see the example of adding value to pointer variable.
#include <stdio.h>
void main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+3;
Output
Address of p variable is 3214864300
After subtracting 3: Address of p variable is 3214864288
You can see after subtracting 3 from pointer variable, it is 12 (4*3) less than the previous
address value.
Here variable arr will give the base address, which is a constant pointer pointing to the
element, arr[0]. Therefore arr is containing the address of arr[0] i.e 1000.
arr is equal to &arr[0] // by default
We can declare a pointer of type int to point to the array arr
Now we can access every element of array arr using p++ to move from one element to
another.
Pointer to Array
As studied above, we can use a pointer to point to an Array, and then we can use that pointer
to access the array. Lets have an example,
In the above program, the pointer *p will print all the values stored in the array one by one.
In the second approach memory wastage is more, hence it is preferred to use pointer in such
cases.
address
of
memory
values.
So,
the
passed
to
function
and
to
the
address
of num1 and num2 respectively. When, the value of pointer are changed, the value in
memory location also changed correspondingly. Hence, change made to *a and *b was
reflected in num1 and num2 in main function.
used in array.
Now let's have a quick look at the methods used for dynamic memory allocation.
malloc()
calloc()
realloc()
free()
malloc() function in C
We can also use scanf() to give values to structure members through terminal.
scanf(" %s ", b1.name);
scanf(" %d ", &b1.price);
Structure Initialization
Like any other data type, structure variable can also be initialized at compile time.
Struct Book b3 = {c++,200,100}; or //initialization
Struct Book b4;
b4.name = java;
b4.price = 250;
b4.pages = 170;
Keyword typedef while using structure
typedef is a keyword used in C language to assign alternative names to existing types. Its
mostly used with user defined data types, when names of data types get slightly complicated.
Following is the general syntax for using typedef,
typedef existing_name alias_name
Lets take an example and see how typedef actually works.
typedef struct complex{
int imag;
float real;
}comp;
Inside main you can define as:
comp c1,c2;
Here, typedef keyword is used in creating a type comp(which is of type as struct
complex). Then, two structure variables c1 and c2 are created by this comp type.
2) Embedded structure
We can define structure within the structure also. It requires less code than previous way. But it
can't be used in many structures.
struct Employee {
int id;
char name[20];
struct Date {
int dd;
int mm;
int yyyy;
}doj;
}emp1;
Accessing Nested Structure
We can access the member of nested structure by Outer_Structure.Nested_Structure.member
as given below:
e1.doj.dd
e1.doj.mm
e1.doj.yyyy
C Nested Structure example
Let's see a simple example of nested structure in C language.
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[20];
struct Date {
int dd;
int mm;
int yyyy;
}doj;
}e1;
void main( ) {
e1.id=101;
//storing employee information
strcpy(e1.name, "Sonoo Jaiswal"); //copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
printf( "employee id : %d\n", e1.id); //printing employee information
printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy):%d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.
doj.yyyy);
}
Output:
employee id : 101
employee name : Sonoo Jaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014
3.0 Unions
Unions are conceptually similar to structures. The syntax of union is also similar to that of
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 same time. A union is declared using union keyword.
union employee {
int id;
char name[20];
float salary;
}emp;
This declares a variable emp 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 a union variable, irrespective of its size. The compiler allocates
the storage that is large enough to hold largest variable type in the union. In the union declared
above the member name requires 20 bytes which is largest among the members. Other
members of union will share the same address.
Advantage of union over structure
It occupies less memory because it occupies the memory of largest member only.
Disadvantage of union over structure
It can store data in one member only.
C Union example
#include <stdio.h>
#include <string.h>
union employee
{
int id;
char name[20];