Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Unit-5

POINTER
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer. The size of
the pointer depends on the architecture. However, in 32-bit architecture the size of a
pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an
integer.
1. int n = 10;   
2. int* p = &n; // Variable p of type pointer is pointing to the address of the variable n 
of type integer.   

Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known
as indirection pointer used to dereference a pointer.
1. int *a;//pointer to int  
2. char *c;//pointer to char  

#include<stdio.h>  
int main(){  
int number=50;    
int *p;      
p=&number;//stores the address of number variable    
printf("Address of p variable is %x \n",p); // p contains the address of the number theref
ore printing p gives the address of number.     
printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a poi
nter therefore if we print *p, we will get the value stored at the address contained by p.    
return 0;  
}    
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50

Pointer to array
1. int arr[10];  
2. int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array ar
r.  

Pointer to a function
1. void show (int);  
2. void(*p)(int) = &display; // Pointer p is pointing to the address of a function  

Pointer to structure
1. struct st {  
2.     int i;  
3.     float f;  
4. }ref;  
5. struct st *p = &ref;  

Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
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.
Address Of (&) Operator
The address of operator '&' returns the address of a variable. But, we need to use %u
to display the address of a variable.
#include<stdio.h>  
int main(){  
int number=50;   
printf("value of number is %d, address of number is %u",number,&number);    
return 0;  
}    
Output
value of number is 50, address of number is fff4

Pointer to Array of functions in C


To understand the concept of an array of functions, we must understand the array of
function. Basically, an array of the function is an array which contains the addresses
of functions. In other words, the pointer to an array of functions is a pointer pointing
to an array which contains the pointers to the functions. Consider the following
example.
#include<stdio.h>  
int show();  
int showadd(int);  
int (*arr[3])();  
int (*(*ptr)[3])();  
  
int main ()  
{  
    int result1;  
    arr[0] = show;  
    arr[1] = showadd;  
    ptr = &arr;  
    result1 = (**ptr)();  
    printf("printing the value returned by show : %d",result1);  
    (*(*ptr+1))(result1);  
}  
int show()  
{  
    int a = 65;  
    return a++;  
}  
int showadd(int b)  
{  
    printf("\nAdding 90 to the value returned by show: %d",b+90);  
}  
Output
printing the value returned by show : 65
Adding 90 to the value returned by show: 155

Function pointer as argument in C


Till now, we have seen that in C programming, we can pass the variables as an
argument to a function. We cannot pass the function as an argument to another
function. But we can pass the reference of a function as a parameter by using a
function pointer. This process is known as call by reference as the function parameter
is passed as a pointer that holds the address of arguments. If any change made by
the function using pointers, then it will also reflect the changes at the address of the
passed variable.
Therefore, C programming allows you to create a pointer pointing to the function,
which can be further passed as an argument to the function. We can create a
function pointer as follows:
1. (type) (*pointer_name)(parameter);  
Example program
void display(void (*p)())  
{  
    for(int i=1;i<=5;i++)  
    {  
        p(i);  
    }  
}  
void print_numbers(int num)  
{  
    cout<<num;  
}  
int main()  
{  
    void (*p)(int);     // void function pointer declaration  
    printf("The values are :");  
  display(print_numbers);  
    return 0;  
}  
In the above code,
o We have defined two functions named 'display()' and print_numbers().
o Inside the main() method, we have declared a function pointer named as (*p),
and we call the display() function in which we pass the print_numbers()
function.
o When the control goes to the display() function, then pointer *p contains the
address of print_numbers() function. It means that we can call the
print_numbers() function using function pointer *p.
o In the definition of display() function, we have defined a 'for' loop, and inside
the for loop, we call the print_numbers() function using statement p(i). Here,
p(i) means that print_numbers() function will be called on each iteration of i,
and the value of 'i' gets printed.
Output

File

Why files are needed?


 When a program is terminated, the entire data is lost. Storing in a file will
preserve your data even if the program terminates.
 If you have to enter a large number of data, it will take a lot of time to enter
them all.
However, if you have a file containing all the data, you can easily access
the contents of the file using a few commands in C.
 You can easily move your data from one computer to another without any
changes.

Types of Files
When dealing with files, there are two types of files you should know about:

1. Text files

2. Binary files

1. Text files

Text files are the normal .txt files. You can easily create text files using any
simple text editors such as Notepad.
When you open those files, you'll see all the contents within the file as plain
text. You can easily edit or delete the contents.

They take minimum effort to maintain, are easily readable, and provide the
least security and takes bigger storage space.

2. Binary files

Binary files are mostly the .bin files in your computer.


Instead of storing data in plain text, they store it in the binary form (0's and
1's).

They can hold a higher amount of data, are not readable easily, and
provides better security than text files.

File Operations
In C, you can perform four major operations on files, either text or binary:
1. Creating a new file

2. Opening an existing file

3. Closing a file

4. Reading from and writing information to a file

Working with files


When working with files, you need to declare a pointer of type file. This
declaration is needed for communication between the file and the program.

FILE *fptr;

Opening a file - for creation and edit


Opening a file is performed using the  fopen()  function defined in
the  stdio.h  header file.
The syntax for opening a file in standard I/O is:

ptr = fopen("fileopen","mode");

Closing a File
The file (both text and binary) should be closed after reading/writing.

Closing a file is performed using the  fclose()  function.

fclose(fptr);

Here,  fptr  is a file pointer associated with the file to be closed.
Reading and writing to a text file
For reading and writing to a text file, we use the
functions  fprintf()  and  fscanf().

Getting data using fseek()


If you have many records inside a file and need to access a record at a
specific position, you need to loop through all the records before it to get
the record.

This will waste a lot of memory and operation time. An easier way to get to
the required data can be achieved using  fseek() .
As the name suggests,  fseek()  seeks the cursor to the given record in the
file.

Syntax of fseek()

fseek(FILE * stream, long int offset, int whence);

The first parameter stream is the pointer to the file. The second parameter
is the position of the record to be found, and the third parameter specifies
the location where the offset starts.

Functions for file handling


There are many functions in the C library to open, read, write, search and close the
file. A list of file functions are given below:

No. Function Description

1 fopen() opens new or existing file

2 fprintf() write data into the file


3 fscanf() reads data from the file

4 fputc() writes a character into the file

5 fgetc() reads a character from file

6 fclose() closes the file

7 fseek() sets the file pointer to given position

8 fputw() writes an integer to file

9 fgetw() reads an integer from file

10 ftell() returns current position

11 rewind() sets the file pointer to the beginning of the file

You might also like