Est 102 Module 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

EST 102 MODULE 5

Introduction to Pointers

 A Pointer in C language is a variable which holds the address of another


variable of same data type.
 Pointers are used to access memory and manipulate the address.
 Pointers are one of the most distinct and exciting features of C language. It
provides power and flexibility to the language.
 Whenever a variable is defined in C language, a memory location is assigned
for it, in which its value will be stored. We can easily check this memory
address, using the & symbol.
 If var is the name of the variable, then &var will give it's address.

#include<stdio.h>

void main()

int var = 7;

printf("Value of the variable var is: %d\n", var);

printf("Memory address of the variable var is: %x\n", &var);

OUTPUT:

Value of the variable var is: 7

Memory address of the variable var is: bcc7a00


Concept of Pointers

 Whenever a variable is declared in a program, system allocates a location i.e


an address to that variable in the memory, to hold the assigned value.
 This location has its own address number, which we just saw above.
 Let us assume that system has allocated memory location 80F for a variable a.

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.

Benefits of using pointers


a) Pointers are more efficient in handling Arrays and Structures.
b) Pointers allow references to function and thereby helps in passing of function
as arguments to other functions.
c) It reduces length of the program and its execution time as well.
d) It allows C language to support Dynamic Memory management.

Dynamic Memory Allocation

 Dynamic Memory Allocation can be defined as a procedure in which the size


of a data structure (like Array) is changed during the runtime.
 C provides some functions to achieve these tasks. There are 4 library functions
provided by C defined under <stdlib.h> header file to facilitate dynamic
memory allocation in C programming. They are:
1. malloc(): “malloc” or “memory allocation” method is used to dynamically
allocate a single large block of memory with the specified size.
2. calloc():“calloc” or “contiguous allocation” method is used to dynamically
allocate the specified number of blocks of memory of the specified type
3. free():“free” method is used to dynamically de-allocate the memory. The
memory allocated using functions malloc() and calloc() are not de-allocated
on their own.
4. realloc():“realloc” or “re-allocation” method is used to dynamically
change the memory allocation of a previously allocated memory.

Declaring, Initializing and using a pointer variable

Declaration of Pointer variable


 General syntax of pointer declaration is,

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:

int *ip; // pointer to integer variable

float *fp; // pointer to float variable

double *dp; // pointer to double variable

char *cp; // pointer to char variable

Initialization of Pointer variable

 Pointer Initialization is the process of assigning address of a variable to


a pointer variable.
 Pointer variable can only contain address of a variable of the same data type.
 In C language address operator & is used to determine the address of a
variable.
 The & (immediately preceding a variable name) returns the address of the
variable associated with it.

#include<stdio.h>

void main()

{
int a = 10;

int *ptr; //pointer declaration

ptr = &a; //pointer initialization

 Pointer variables always point to variables of same data-type. Let's have an


example to showcase this:

#include<stdio.h>

void main()

float a;

int *ptr;

ptr = &a; // ERROR, type mismatch

 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;

Using the pointer or Dereferencing of Pointer

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()

int a, *p; // declaring the variable and pointer

a = 10;

p = &a; // initializing the pointer

printf("%d", *p); //this will print the value of 'a'

printf("%d", *&a); //this will also print the value of 'a'

printf("%u", &a); //this will print the address of 'a'

printf("%u", p); //this will also print the address of 'a'

printf("%u", &p); //this will print the address of 'p’

return 0;

}
Points to remember while using pointers:

a) While declaring/initializing the pointer variable, * indicates that the variable


is a pointer.
b) The address of any variable is given by preceding the variable name with
Ampersand &.
c) The pointer variable stores the address of a variable. The declaration int
*a doesn't mean that a is going to contain an integer value. It means that a is
going to contain the address of a variable storing integer value.
d) To access the value of a certain address stored by a pointer variable, * is used.
Here, the * can be read as 'value at'.

Example

#include <stdio.h>

int main()

int i = 10; // normal integer variable storing value 10

int *a; // since '*' is used, hence its a pointer variable

/*

'&' returns the address of the variable 'i'

which is stored in the pointer variable 'a'

*/

a = &i;

/*

below, address of variable 'i', which is stored

by a pointer variable 'a' is displayed


*/

printf("Address of variable i is %u\n", a);

/*

below, '*a' is read as 'value at a'

which is 10

*/

printf("Value at the address, which is stored by pointer variable


a is %d\n", *a);

return 0;

OUTPUT:

Address of variable i is 2686728 (The address may vary)

Value at an address, which is stored by pointer variable a is 10

Pointer to a Pointer (Double Pointer)

 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

Simple program to represent Pointer to a Pointer

#include <stdio.h>

int main() {

int a = 10;

int *p1; //this can store the address of variable a

int **p2;

/*

this can store the address of pointer variable p1 only.

It cannot store the address of variable 'a'

*/

p1 = &a;

p2 = &p1;

printf("Address of a = %u\n", &a);

printf("Address of p1 = %u\n", &p1);

printf("Address of p2 = %u\n\n", &p2);

// below print statement will give the address of 'a'

printf("Value at the address stored by p2 = %u\n", *p2);

printf("Value at the address stored by p1 = %d\n\n", *p1);


printf("Value of **p2 = %d\n", **p2); //read this *(*p2)

/*

This is not allowed, it will give a compile time error-

p2 = &a;

printf("%u", p2);

*/

return 0;

OUTPUT:

Address of a = 2686724

Address of p1 = 2686728

Address of p2 = 2686732

Value at the address stored by p2 = 2686724

Value at the address stored by p1 = 10

Value of **p2 = 10

Explanation of the above program:


 p1 pointer variable can only hold the address of the variable a (i.e Number of
indirection operator(*)-1 variable). Similarly, p2 variable can only hold the
address of variable p1. It cannot hold the address of variable a.
 *p2 gives us the value at an address stored by the p2 pointer. p2 stores the
address of p1pointer and value at the address of p1 is the address of variable a.
Thus, *p2 prints address of a.
 **p2 can be read as *(*p2). Hence, it gives us the value stored at the
address *p2. From above statement, you know *p2 means the address of
variable a. Hence, the value at the address *p2 is 10. Thus, **p2 prints 10.

Pointer and Arrays

 When an array is declared, compiler allocates sufficient amount of memory to


contain all the elements of the array. Base address i.e address of the first
element of the array is also allocated by the compiler.

Suppose we declare an array arr,

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.

arr is equal to &arr[0] by default

 We can also declare a pointer of type int to point to the array arr.

int *p;

p = arr;

// or,

p = &arr[0]; //both the statements are equivalent.

 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;

int a[5] = {1, 2, 3, 4, 5};

int *p = a; // same as int*p = &a[0]


for (i = 0; i < 5; 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]

Pointer to Multidimensional Array

 A multidimensional array is of form, a[i][j]. Lets see how we can make a


pointer point to such an array. As we know now, name of the array gives its
base address.
 In a[i][j], a will give the base address of this array, even a + 0 + 0 will also
give the base address, that is the address of a[0][0]element.
 Here is the generalized form for using pointer with multidimensional arrays.

*(*(a + i) + j)

which is same as,

a[i][j]

Pointer and Character strings

 Pointer can also be used to create strings. Pointer variables of char type are
treated as string.

char *str = "Hello";

 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;

str = "hello"; //this is Legal

 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"

};

//Now lets see same array without using pointer

char name[3][20] = {

"Adam",

"chris",
"Deniel"

};

 In the second approach memory wastage is more, hence it is prefered to use


pointer in such cases.
 When we say memory wastage, it doesn't means that the strings will start
occupying less space, no, characters will take the same space, but when we
define array of characters, a contiguous memory space is located equal to the
maximum size of the array, which is a wastage, which can be avoided if we
use pointers instead.
FILES IN C

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.

In C language, we use a structure pointer of file type to declare a file.

FILE *fp;

C provides a number of functions that helps to perform basic file operations.


Following are the functions,

Function Description

fopen() create a new file or open a existing file

fclose() closes a file

getc() reads a character from a file

putc() writes a character to a file

fscanf() reads a set of data from a file

fprintf() writes a set of data to a file

getw() reads a integer from a file


putw() writes a integer to a file

fseek() set the position to desire point

ftell() gives current position in the file

rewind() set the position to the begining point

Opening a File or Creating a File

The fopen() function is used to create a new file or to open an existing file.

General Syntax:

*fp = FILE *fopen(const char *filename, const char *mode);

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

r opens a text file in reading mode

w opens or create a text file in writing mode.

a opens a text file in append mode


r+ opens a text file in both reading and writing mode

w+ opens a text file in both reading and writing mode

a+ opens a text file in both reading and writing mode

rb opens a binary file in reading mode

wb opens or create a binary file in writing mode

ab opens a binary file in append mode

rb+ opens a binary file in both reading and writing mode

wb+ opens a binary file in both reading and writing mode

ab+ opens a binary file in both reading and writing mode

Closing a File

The fclose() function is used to close an already opened file.

General Syntax :

int fclose( FILE *fp);

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.

Input/Output operation on File


In the above table we have discussed about various file I/O functions to perform
reading and writing on file. getc() and putc() are the simplest functions which can
be used to read and write individual characters to a file.

#include<stdio.h>

int main()

FILE *fp;

char ch;

fp = fopen("one.txt", "w");

printf("Enter data...");

while( (ch = getchar()) != EOF) {

putc(ch, fp);

fclose(fp);

fp = fopen("one.txt", "r");

while( (ch = getc(fp)! = EOF)

printf("%c",ch);

// closing the file pointer

fclose(fp);

return 0;

Reading and Writing to File using fprintf() and fscanf()


#include<stdio.h>

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");

printf("Enter Name and Age:");

scanf("%s %d", e.name, &e.age);

fprintf(p,"%s %d", e.name, e.age);

fclose(p);

do

fscanf(q,"%s %d", e.name, e.age);

printf("%s %d", e.name, e.age);

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.

Difference between Append and Write Mode

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.

Formatted and Unformatted Input/Output


 Unformatted Input/Output is the most basic form of input/output.
Unformatted input/output transfers the internal binary representation of the
data directly between memory and the file.
 Formatted output converts the internal binary representation of the data to
ASCII characters which are written to the output file.
 Formatted input reads characters from the input file and converts them to
internal form. Formatted I/O can be either "Free" format or "Explicit" format.

Advantages and Disadvantages of Unformatted I/O

 Unformatted input/output is the simplest and most efficient form of


input/output.
 It is usually the most compact way to store data.
 Unformatted input/output is the least portable form of input/output.
Unformatted data files can only be moved easily to and from computers that
share the same internal data representation.
 It should be noted that XDR (eXternal Data Representation) files, can be used
to produce portable binary data.
 Unformatted input/output is not directly human readable, so you cannot type
it out on a terminal screen or edit it with a text editor.

Advantages and Disadvantages of Formatted I/O

 Formatted input/output is very portable. It is a simple process to move


formatted data files to various computers, even computers running different
operating systems, as long as they all use the ASCII character set
 Formatted files are human readable and can be typed to the terminal screen or
edited with a text editor
 However, formatted input/output is more computationally expensive than
unformatted input/output because of the need to convert between internal
binary data and ASCII text.
 Formatted data requires more space than unformatted to represent the same
information. Inaccuracies can result when converting data between text and
the internal representation.
Free Format I/O

With free format input/output, IDL uses default rules to format the data.

Advantages and Disadvantages of Free Format I/O

 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

Explicit format I/O allows you to specify the exact format for input/output.

Advantages and Disadvantages of Explicit I/O

 Explicit formatting allows a great deal of flexibility in specifying exactly how


data will be formatted.
 Formats are specified using a syntax that is similar to that used in FORTRAN
format statements.
 Scientists and engineers already familiar with FORTRAN will find IDL
formats easy to write.
 Commonly used FORTRAN format codes are supported. In addition, IDL
formats have been extended to provide many of the capabilities found in
the scanf () and printf () functions commonly found in the C language runtime
library.
 However, there are some disadvantages to using Explicit I/O. Using explicitly
specified formats requires the user to specify more detail-they are, therefore,
more complicated to use than free format.
 The type of input/output to use in a given situation is usually determined by
considering the advantages and disadvantages of each method as they relate
to the problem to be solved.
 Also, when transferring data to or from other programs or systems, the type
of input/output is determined by the application.
 The following suggestions are intended to give a rough idea of the issues
involved, though there are always exceptions:
a) Images and large data sets are usually stored and manipulated using
unformatted input/output in order to minimize processing overhead. The IDL
ASSOC function is often the natural way to access such data.
b) Data that need to be human readable should be written using formatted
input/output.
c) Data that need to be portable should be written using formatted input/output.
Another option is to use unformatted XDR files by specifying the XDR
keyword with the OPEN procedures. This is especially important if moving
between computers with markedly different internal binary data formats.
d) Free format input/output is easier to use than explicitly formatted input/output
and about as easy as unformatted input/output, so it is often a good choice for
small files where there is no strong reason to prefer one method over another.
e) Special well-known complex file formats are usually supported directly with
special IDL routines (e.g. READ_JPEG for JPEG images)
Command Line Argument in C
 Command line argument is a parameter supplied to the program when it is
invoked.
 Command line argument is an important concept in C programming.
 It is mostly used when you need to control your program from outside.
 Command line arguments are passed to the main() method.
 Syntax:

int main(int argc, char *argv[])

 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.

Example for Command Line Argument

#include <stdio.h>

#include <conio.h>

int main(int argc, char *argv[])

int i;

if( argc >= 2 )

printf("The arguments supplied are:\n");


for(i = 1; i < argc; i++)

printf("%s\t", argv[i]);

else

printf("argument list is empty.\n");

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

You might also like