Est 102 Module 5
Est 102 Module 5
Est 102 Module 5
Introduction to Pointers
#include<stdio.h>
void main()
int var = 7;
OUTPUT:
int a = 10;
We can access the value 10 either by using the variable name a or by using its
address 80F.
The variables which are used to hold memory addresses are called Pointer
variables.
A pointer variable is therefore nothing but a variable which holds an address
of some other variable.
And the value of a pointer variable gets stored in another memory location.
datatype *pointer_name;
Data type of a pointer must be same as the data type of the variable to which
the pointer variable is pointing.
void type pointer works with all data types, but is not often used.
Here are a few examples:
#include<stdio.h>
void main()
{
int a = 10;
#include<stdio.h>
void main()
float a;
int *ptr;
If you are not sure about which variable's address to assign to a pointer
variable while declaration, it is recommended to assign a NULL value to your
pointer variable.
A pointer which is assigned a NULLvalue is called a NULL pointer.
#include <stdio.h>
int main()
{
int *ptr = NULL;
return 0;
Once a pointer has been assigned the address of a variable, to access the value of
the variable, pointer is dereferenced, using the indirection
operator or dereferencing operator *.
#include <stdio.h>
int main()
a = 10;
return 0;
}
Points to remember while using pointers:
Example
#include <stdio.h>
int main()
/*
*/
a = &i;
/*
/*
which is 10
*/
return 0;
OUTPUT:
Pointers are used to store the address of other variables of similar data-type.
But if you want to store the address of a pointer variable, then you again need
a pointer to store it.
Thus, when one pointer variable stores the address of another pointer variable,
it is known as Pointer to Pointer variable or Double Pointer.
Syntax:
int **p1;
Here, we have used two indirection operator(*) which stores and points to
the address of a pointer variable i.e, int *.
If we want to store the address of this (double pointer) variable p1, then the
syntax would become:
int ***p2
#include <stdio.h>
int main() {
int a = 10;
int **p2;
/*
*/
p1 = &a;
p2 = &p1;
/*
p2 = &a;
printf("%u", p2);
*/
return 0;
OUTPUT:
Address of a = 2686724
Address of p1 = 2686728
Address of p2 = 2686732
Value of **p2 = 10
int arr[5] = { 1, 2, 3, 4, 5 };
Assuming that the base address of arr is 1000 and each integer requires two
bytes, the five elements will be stored as follows:
Here variable arr will give the base address, which is a constant pointer
pointing to the first element of the array, arr[0]. Hence arr contains the address
of arr[0] i.e 1000.
In short, arr has two purpose - it is the name of the array and it acts as a pointer
pointing towards the first element in the array.
We can also declare a pointer of type int to point to the array arr.
int *p;
p = arr;
// or,
Now we can access every element of the 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 elements. Lets have an example,
#include <stdio.h>
int main()
int i;
printf("%d", *p);
p++;
return 0;
In the above program, the pointer *p will print all the values stored in the
array one by one. We can also use the Base address (a in above case) to act
as a pointer and print all the values.
The generalized form for using pointer with an array,
*(a+i)
is same as:
a[i]
*(*(a + i) + j)
a[i][j]
Pointer can also be used to create strings. Pointer variables of char type are
treated as string.
The above code creates a string and stores its address in the pointer
variable str.
The pointer str now points to the first character of the string "Hello".
Another important thing to note here is that the string created
using char pointer can be assigned a value at runtime.
char *str;
The content of the string can be printed using printf() and puts().
printf("%s", str);
puts(str);
Notice that str is pointer to the string, it is also name of the string. Therefore
we do not need to use indirection operator *.
Array of Pointers
We can also have array of pointers. Pointers are very helpful in handling
character array with rows of varying length.
char *name[3] = {
"Adam",
"chris",
"Deniel"
};
char name[3][20] = {
"Adam",
"chris",
"Deniel"
};
File Input/Output in C
A file represents a sequence of bytes on the disk where a group of related data is
stored. File is created for permanent storage of data.
FILE *fp;
Function Description
The fopen() function is used to create a new file or to open an existing file.
General Syntax:
Here, *fp is the FILE pointer (FILE *fp), which will hold the reference to the opened
(or created) file.
filename is the name of the file to be opened and mode specifies the purpose of
opening the file. Mode can be of following types,
mode Description
Closing a File
General Syntax :
Here fclose() function closes the file and returns zero on success, or EOF if there is
an error in closing the file. This EOF is a constant defined in the header file stdio.h.
#include<stdio.h>
int main()
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
putc(ch, fp);
fclose(fp);
fp = fopen("one.txt", "r");
printf("%c",ch);
fclose(fp);
return 0;
struct emp
char name[10];
int age;
};
void main()
struct emp e;
FILE *p,*q;
p = fopen("one.txt", "a");
q = fopen("one.txt", "r");
fclose(p);
do
while(!feof(q));
}
In this program, we have created two FILE pointers and both are refering to the same
file but in different modes.
fprintf() function directly writes into the file, while fscanf() reads from the file,
which can then be printed on the console using standard printf() function.
Write (w) mode and Append (a) mode, while opening a file are almost the same.
Both are used to write in a file. In both the modes, new file is created if it doesn't
exists already.
The only difference they have is, when you open a file in the write mode, the file is
reset, resulting in deletion of any data already present in the file. While the append
mode is used to append or add data to the existing data of file(if any). Hence, when
you open a file in Append(a) mode, the cursor is positioned at the end of the present
data in the file.
With free format input/output, IDL uses default rules to format the data.
The user is free of the chore of deciding how the data should be formatted.
Free format is extremely simple and easy to use.
It provides the ability to handle the majority of formatted input/output needs
with a minimum of effort.
However, the default formats used are not always exactly what is required. In
this case, explicit formatting is necessary.
Explicit format I/O allows you to specify the exact format for input/output.
Here argc counts the number of arguments on the command line and argv[ ] is
a pointer array which holds pointers of type char which points to the
arguments passed to the program.
#include <stdio.h>
#include <conio.h>
int i;
printf("%s\t", argv[i]);
else
return 0;
Remember that argv[0] holds the name of the program and argv[1] points to
the first command line argument and argv[n] gives the last argument.
If no argument is supplied, argc will be 1