Module 5 Pointers & Files
Module 5 Pointers & Files
Module 5 Pointers & Files
The pointers perform the function of storing the addresses of other variables in the program.
These variables could be of any type- char, int, function, array, or other pointers. The pointer sizes
depend on their architecture. But, keep in mind that the size of a pointer in the 32-bit architecture
is 2 bytes.
Example.
int x = 10;
int* p = &x;
Here, the variable p is of pointer type, and it is pointing towards the address of the x variable,
which is of the integer type.
Consider that we are declaring a variable “a” of int type, and it will store a value of zero.
int a = 0
Declaration of a Pointer
Just like the variables, we also have to declare the pointers in C before we use them in any
program. We can name these pointers anything that we like, as long as they abide by the naming
rules of the C language. Normally, the declaration of a pointer would take a form like this:
data_type * name_of_pointer_variable;
The data_type refers to this pointer’s base type in the variable of C. It indicates which
type of variable is the pointer pointing to in the code.
The asterisk ( * ) is used to declare a pointer. It is an indirection operator, and it is the
same asterisk that we use in multiplication.
We can declare pointers in the C language with the use of the asterisk symbol ( * ). It is also
called the indirection pointer, as we use it for dereferencing any pointer. Here is how we use it:
Once we declare a pointer, we then initialise it just like the standard variables using an address of
the variable. In case we don’t initialise the pointers in any C program and start using it directly,
the results can be pretty unpredictable and potentially disastrous.
We use the & (ampersand) operator to get the variable’s address in the program. We place the &
just before that variable’s name whose address we require. Here is the syntax that we use for the
initialisation of a pointer,
pointer = &variable;
Let us look at an example of how we can use pointers for printing the addresses along with the
value.
#include<stdio.h>
int main(){
int digit=100;
int *r;
r=&digit;
return 0;
The output
OPERATORS
void(*q)(int) = &show; // The q pointer is pointing towards the function’s address in the program
struct str {
int x;
float y;
}ref;
Types of Pointers
There are various types of pointers that we can use in the C language. Let us take a look at the
most important ones.
The Null Pointer
The null pointer has a value of 0. To create a null pointer in C, we assign the pointer with a null
value during its declaration in the program. This type of method is especially useful when the
pointer has no address assigned to it. Let us take a look at a program that illustrates how we use
the null pointer:
#include <stdio.h>
int main()
return 0;
The output obtained out of the program mentioned above will be:
The void pointer is also known as the generic pointer in the C language. This pointer has no
standard data type, and we create it with the use of the keyword void. The void pointer is
generally used for the storage of any variable’s address. Let us take a look at a program that
illustrates how we use the void pointer in a C program:
#include <stdio.h>
int main()
return 0;
}
The output obtained out of the program mentioned above will be:
We can call a pointer a wild pointer if we haven’t initialized it with anything. Such wild pointers
are not very efficient in a program, as they may ultimately point towards some memory location
that is unknown to us. It will ultimately lead to some problems in the program, and then, it will
crash. Thus, you must be very careful when using wild pointers in a program. Let us take a look
at a program that illustrates how we use the wild pointer in a C program:
#include <stdio.h>
int main()
printf(“\n%d”,*w);
return 0;
The output obtained out of the program mentioned above will be a compile-time error.
Here are a few more types of pointers that you can find in the C programs:
Near pointer
Complex pointer
Huge pointer
Far pointer
Dangling pointer
Accessing Pointers- Indirectly and Directly
There are basically two ways in which a program can access a variable content and then
manipulate it:
Direct Access: In this case, we use the variable name in the program directly.
Indirect Access: In this case, we use some pointers to the variables in the program.
Let us take a look at a program to understand this better. Consider the below program:
#include <stdio.h>
int variable = 1;
int *ptr;
ptr = &variable;
*ptr=48;
return 0;
The compilation of this program devoid of errors would generate an output like this:
The direct access of the variable would be = 1
They provide an efficient way in which we can access the elements present in the
structure of an array.
These are also used to create complex data structures, like the linked list, tree, graph, etc.
It reduces the code and thus improves the overall performance. We can use it to retrieve
the strings, trees, and many more. We can also use it with structures, arrays, and
functions.
We can use the pointers for returning various values from any given function.
One can easily access any location of memory in the computer using the pointers.
Cons of Pointers in C
These can lead to some errors like segmentation faults, accessing of unrequired memory
locations, and many more.
Some programmers might find it very tricky to deal with pointers in a program. It is their
responsibility to use these pointers and manipulate them carefully.
The & Address of Operator in C
This operator basically returns the variable’s address. But keep in mind that we must use the %u
if we want to display the variable’s address.
#include<stdio.h>
int main(){
int digit=100;
printf(“The value of the digit is equal to %d and the address of the digit is equal to
%u”,digit,&digit);
return 0;
The value of the digit is equal to 100 and the address of the digit is equal to fff4
File Handling in C
File handling in C enables us to create, update, read, and delete the files stored on
the local file system through our C program. The following operations can be
performed on a file.
We must open a file before it can be read, write, or update. The fopen() function is
used to open a file. The syntax of the fopen() is given below.
The fclose() function is used to close a file. The file must be closed after performing
all the operations on it. The syntax of fclose() function is given below:
1. int fclose( FILE *fp );
EXAMPLE
1. #include<stdio.h>
2. void main( )
3. {
4. FILE *fp ;
5. char ch ;
6. fp = fopen("file_handle.c","r") ;
7. fclose (fp ) ;
8. }
Output
C fseek() function
The fseek() function is used to set the file pointer to the specified offset. It is used to
write data into file at desired location.
Syntax:
1. int fseek(FILE *stream, long int offset, int whence)
There are 3 constants used in the fseek() function for whence: SEEK_SET, SEEK_CUR
and SEEK_END.
Example:
1. #include <stdio.h>
2. void main(){
3. FILE *fp;
4.
5. fp = fopen("myfile.txt","w+");
6. fputs("This is javatpoint", fp);
7.
8. fseek( fp, 7, SEEK_SET );
9. fputs("sonoo jaiswal", fp);
10. fclose(fp);
11. }
myfile.txt
C rewind() function
The rewind() function sets the file pointer at the beginning of the stream. It is useful
if you have to use stream many times.
Syntax:
1. void rewind(FILE *stream)
Example:
File: file.txt
1. this is a simple text
File: rewind.c
1. #include<stdio.h>
2. #include<conio.h>
3. void main(){
4. FILE *fp;
5. char c;
6. clrscr();
7. fp=fopen("file.txt","r");
8.
9. while((c=fgetc(fp))!=EOF){
10. printf("%c",c);
11. }
12.
13. rewind(fp);//moves the file pointer at beginning of the file
14.
15. while((c=fgetc(fp))!=EOF){
16. printf("%c",c);
17. }
18.
19. fclose(fp);
20. getch();
21. }
Output:
C ftell() function
The ftell() function returns the current file position of the specified stream. We can
use ftell() function to get the total size of a file after moving file pointer at the end of
file. We can use SEEK_END constant to move the file pointer at the end of file.
Syntax:
1. long int ftell(FILE *stream)
Example:
File: ftell.c
Play Video
1. #include <stdio.h>
2. #include <conio.h>
3. void main (){
4. FILE *fp;
5. int length;
6. clrscr();
7. fp = fopen("file.txt", "r");
8. fseek(fp, 0, SEEK_END);
9.
10. length = ftell(fp);
11.
12. fclose(fp);
13. printf("Size of file: %d bytes", length);
14. getch();
15. }
Output:
Example
struct emp{
int eno;
float sal;
} e;
FILE *fp;
pointer);
Example
struct emp{
int eno:
float sal;
} e;
FILE *fp;
feof()
The function feof() is used to check the end of file after EOF. It tests the end of file indicator.
It returns non-zero value if successful otherwise, zero.
Example
#include <stdio.h>
int main() {
int c = getc(f);
while (c != EOF) {
putchar(c);
c = getc(f);
}
if (feof(f))
else
printf("\n Failure.");
fclose(f);
getchar();
return 0;
Output
This is demo!
This is demo!
Lookup table
In computer science, a lookup table (LUT) is an array that replaces runtime computation
with a simpler array indexing operation. The process is termed as "direct addressing" and
LUTs differ from hash tables in a way that, to retrieve a value with key , a hash table would
store the value in the slot where is a hash function i.e. is used to compute the slot, while in
the case of LUT, the value is stored in slot , thus directly addressable.
typedef in C
The typedef is a keyword used in C programming to provide some meaningful
names to the already existing variable in the C program
. It
behaves similarly as we define the alias for the commands. In
short, we can say that this keyword is used to redefine the name of
an already existing variable.
Syntax of typedef
1. typedef <existing_name> <alias_name>
2. #include <stdio.h>
3. int main()
4. {
5. typedef unsigned int unit;
6. unit i,j;
7. i=10;
8. j=20;
9. printf("Value of i is :%d",i);
10. printf("\nValue of j is :%d",j);
11. return 0;
12. }