Assignment 2
Assignment 2
Assignment 2
Pointer Basics
•Normal variable stores the value whereas pointer variable stores the address of the variable.
•* symbol is used to get the value of the variable that the pointer is pointing to.
• Pointer reduces the code and improves the performance, it is used to retrieving strings, trees
etc. and used with arrays, structures and functions.
It makes you able to access any memory location in the computer's memory.
Pointers can be used to return multiple values from a function via function arguments.
Pointers permit references to functions and thereby facilitating passing of function as arguments
to other functions.
The use of pointers arrays to character stings results in saving of data storage space in memory.
Pointers provide an efficient tool for manipulating dynamic data structures such as structures,
linked lists, queues, stacks and trees.
Pointers reduce length and complexity of programs.
Advantages of Pointers
Pointers are used to return more than one value from a function.
Pointer allows to create complex data structures such is linked list, stack, queues, trees, graphs
etc.
Usage of pointer
In c language, we can dynamically allocate memory using malloc() and calloc() functions where
the pointer is used.
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code
and improves the performance.
Disadvantages of Pointers
. [The compiler has no idea what type of object a void Pointer really points to ?]
Dereferencing a pointer
Dereferencing means retrieving the data from the memory address stored in the pointer
2. ptr[0] - treat the pointer as an array name and access its first element
• Array names are actually pointers! int intArray[10]; // declare an integer array intArray is a
pointer!
Pointer Arithmetic
► Therefore, we can perform arithmetic operations on a pointer just as you can on a numeric
value.
A limited set of arithmetic operations can be performed on pointers. A pointer may be:
1. Incremented (++)
2. Decremented (-)
3. An integer may be added to a pointer (+or+=)
Pointers contain addresses. Adding two addresses makes no sense, because there is no idea
what it would point to.
Subtracting two addresses lets you compute the offset between these two addresses.
Files Module 6
Introduction to Files in C:
A File is a collection of data stored in the secondary memory.
So far data was entered into the programs through the keyboard.
So Files are used for storing information that can be processed by the programs.
Files are not only used for storing the data, programs are also stored in files.
In order to use files, we have to learn file input and output operations.
That is, how data is read and how to write into a file.
A Stream is the important concept in C. The Stream is a common, logical interface to the
various devices that comprise the computer. So a Stream is a logical interface to a file. There
are two types of Streams, Text Stream and Binary Stream.
A Text File can be thought of as a stream of characters that can be processed sequentially. It
can only be processed in the forward direction.
Using Files in C:
To use a file four essential actions should be carried out. These are,
a. Declare a file pointer variable.
b. Open a file using the fopen() function.
c. Process the file using suitable functions.
d. Close the file using the fclose() and fflush() functions.
Opening a file
To open a file using the fopen() function. Its syntax is,
FILE *fopen(const char *fname,const char* mode);
const char *fname represents the file name. The file name is like “D:\\501\example.txt”.
Here D: is a drive, 501 is the directory, example.txt is a file name.
const char *mode represents the mode of the file. It has the following values.
Creating a File
In C. It can be freely, overwriting file using fopen() with "w" (write) or "a" (append) modes. The
"w" mode starts en an empty file, Overwriting existing content, or creates a new file if it doesn't)
modes. The "w" mode starts ts to the end of the file without altering existing content, or creates
a new test. The a mode adds new choose whether to overwrite or append data, offering
flexibility in file management)
fgetc()
Reads a single character from a file.
fgets()
Reads a string from a file.
fread()
Reads a block of data from a file.).
********