Assignment 2

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

Assignment 2

Pointers and files

Pointer Basics

•Normal variable stores the value whereas pointer variable stores the address of the variable.

•The content of the C pointer always be a whole number i.e. address.

•Always C pointer is initialized to null, i.e. int *p = null.

•The value of null pointer is 0.

•& symbol is used to get the address of the variable.

•* symbol is used to get the value of the variable that the pointer is pointing to.

•If a pointer in C is assigned to NULL, it means it is pointing to nothing.

Benefits of using pointers

• Pointer reduces the code and improves the performance, it is used to retrieving strings, trees
etc. and used with arrays, structures and functions.

• We can return multiple values from function using pointer.

It makes you able to access any memory location in the computer's memory.

Pointers are more efficient in handling arrays and data tables.

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 allow C to support dynamic memory management.

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 provide direct access to memory.

Pointers are used to return more than one value from a function.

Pointers allow dynamic memory allocation and deallocation.

• Reduces the storage space required by the program.

Pointers are more efficient in handling arrays and structures.

Pointers are used to get reference of a variable or function.

• Pointers increases program execution speed.

Pointer allows to create complex data structures such is linked list, stack, queues, trees, graphs
etc.

Usage of pointer

There are many applications of pointers in c language.

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and calloc() functions where
the pointer is used.

2) Arrays, Functions, and Structures

Pointers in c language are widely used in arrays, functions, and structures. It reduces the code
and improves the performance.

Disadvantages of Pointers

• Pointers are a little complex to understand.

• If an incorrect value is provided to a pointer, it may cause memory corruption.

• Pointers are also responsible for memory leakage.


• Programmers find it very difficult to work with the pointers; therefore it is programmer's
responsibility to manipulate a pointer carefully.

Basic of void pointer

In C General Purpose Pointer is called as void Pointer.

It can store address of any type of variable

. [The compiler has no idea what type of object a void Pointer really points to ?]

■ Memory required for void pointer is 8 byte

Void pointer increment by I byte

It does not have any data type associated with it.

Dereferencing a pointer
Dereferencing means retrieving the data from the memory address stored in the pointer

• Two ways to dereference

If "ptr" has been declared as a pointer variable

1. *ptr - use the "*" operator

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

A pointer in c is an address, which is a numeric value.

► 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+=)

4. An integer may be subtracted from 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.

Declaration of file pointer:


A pointer variable is used to points a structure FILE. The members of the FILE structure are
used by the program in various file access operation, but programmers do not need to
concerned about them.
FILE *file_pointer_name;
Eg : FILE *fp;

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.

Closing and Flushing Files


The file must be closed using the fclose() function. Its prototype is ,
int fclose(FILE *fp);
The argument fp is the FILE pinter associated with the stream. Fclose() returns 0 on success
and -1 on error.
fflush() used when a file’s buffer is to be written to disk while still using the file.
Use of fflushall() to flush the buffers of all open streams. The prototype of these two functions
are,
int flushall(void);
int fflush(FILE *fp);

fclose(file) is called to close the file

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)

Reading from a File

To read from a file, use functions like fgeto(), fgets(), andfread().

fgetc()
Reads a single character from a file.
fgets()
Reads a string from a file.
fread()
Reads a block of data from a file.).

********

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy