The document discusses different types of pointers in C including null pointers, void pointers, and wild pointers. It also describes direct and indirect accessing of pointers and provides examples. Finally, it covers common file handling functions in C like fopen(), fclose(), fprintf(), and fscanf() to open, read, write, and close files, giving examples of storing employee data in a file.
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views
Unit 3pointer2
The document discusses different types of pointers in C including null pointers, void pointers, and wild pointers. It also describes direct and indirect accessing of pointers and provides examples. Finally, it covers common file handling functions in C like fopen(), fclose(), fprintf(), and fscanf() to open, read, write, and close files, giving examples of storing employee data in a file.
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21
Types of Pointers
Accessing Pointers- Indirectly and
Directly File Handling in C Dr. Shalini Gambhir Types of Pointers • There are various types of pointers that we can use in the C language. • 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. • Program that illustrates how to use the null pointer: • #include <stdio.h> • int main() • { • int *a = NULL; // the null pointer declaration • printf(“Value of the variable a in the program is equal to :\n%x”,a); • return 0; • } • Output • Value of the variable a in the program is equal to: 0 The Void Pointer • 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. • Program that illustrates how we use the void pointer in a C program: • #include <stdio.h> • int main() • { • void *q = NULL; // the void pointer of the program • printf(“Size of the void pointer in the program is equal to : %d\ n”,sizeof(q)); • return 0; • } • The output obtained out of the program mentioned above will be: • Size of the void pointer in the program is equal to : 4 The Wild Pointer • 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. • Program that illustrates how we use the wild pointer in a C program: • #include <stdio.h> • int main() • { • int *w; // the wild pointer in the program • printf(“\n%d”,*w); • return 0; • } • The output obtained out of the program mentioned above will be a compile-time error. 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. Example: • #include <stdio.h> • /* Declaration and initialization of an int variable in the program */ • int variable = 1; • /* Declaration of the pointer to the int variable */ • int *ptr; • int main( void ) • { • /* Initialization of ptr to the point to the variable */ • ptr = &variable; • /* Accessing var indirectly and directly */ • printf(“\nThe direct access of the variable would be = %d”, variable); • printf(“\nThe indirect access of the variable would be = %d”, *ptr); • /* Displaying of the address of the variable in both the ways */ • printf(“\n\nOutput of the address of the variable would be = %d”, &variable); • printf(“\nOutput of the address of the variable would be = %d\n”, ptr); • /*changing of the content of the variable through the ptr pointer in the program*/ • *ptr=48; • printf(“\nThe indirect access of the variable would be = %d”, *ptr); • 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 • The indirect access of the variable would be = 1 • Output of the address of the variable would be = 4202496 • Output of the address of the variable would be = 4202496 • The indirect access of the variable would be = 48 File Handling in C • In programming, we may require some specific input data to be generated several numbers of times. Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again. However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of 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. • Creation of the new file • Opening an existing file • Reading from the file • Writing to the file • Deleting the file • 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: Opening File: fopen() • 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. • FILE *fopen( const char * filename, const char * mode ); • The fopen() function accepts two parameters: • The file name (string). If the file is stored at some specific location, then we must mention the path at which the file is stored. For example, a file name can be like "c://some_folder/some_file.ext". • The mode in which the file is to be opened. It is a string. We can use one of the following modes in the fopen() function: • The fopen function works in the following way. • Firstly, It searches the file to be opened. • Then, it loads the file from the disk and place it into the buffer. The buffer is used to provide efficiency for the read operations. • It sets up a character pointer which points to the first character of the file. Example which opens a file in write mode: • #include<stdio.h> • void main( ) • { • FILE *fp ; • char ch ; • fp = fopen("file_handle.c","r") ; • while ( 1 ) • { • ch = fgetc ( fp ) ; • if ( ch == EOF ) • break ; • printf("%c",ch) ; • } • fclose (fp ) ; • } • Output • The content of the file will be printed. Closing File: fclose() • 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: • int fclose( FILE *fp ); C fprintf() and fscanf() • Writing File : fprintf() function • The fprintf() function is used to write set of characters into file. It sends formatted output to a stream. • Syntax: • int fprintf(FILE *stream, const char *format [, argument, ...]) Example: • #include <stdio.h> • main(){ • FILE *fp; • fp = fopen("file.txt", "w");//opening file • fprintf(fp, "Hello file by fprintf...\n");// writing data into file • fclose(fp);//closing file • } Reading File : fscanf() function • The fscanf() function is used to read set of characters from file. It reads a word from the file and returns EOF at the end of file. • Syntax: • int fscanf(FILE *stream, const char *format [, argument, ...]) Example: • #include <stdio.h> • main(){ • FILE *fp; • char buff[255];//creating char array to store data of file • fp = fopen("file.txt", "r"); • while(fscanf(fp, "%s", buff)!=EOF){ • printf("%s ", buff ); • } • fclose(fp); • } • Output: • Hello file by fprintf... C File Example: Storing employee information • #include <stdio.h> • void main() • { • FILE *fptr; • int id; • char name[30]; • float salary; • fptr = fopen("emp.txt", "w+");/* open for writing */ • if (fptr == NULL) • { • printf("File does not exists \n"); • return; • } • printf("Enter the id\n"); • scanf("%d", &id); • fprintf(fptr, "Id= %d\n", id); • printf("Enter the name \n"); • scanf("%s", name); • fprintf(fptr, "Name= %s\n", name); • printf("Enter the salary\n"); • scanf("%f", &salary); • fprintf(fptr, "Salary= %.2f\n", salary); • fclose(fptr); • } • Output: • Enter the id 1 Enter the name sonoo Enter the salary 120000 • Now open file from current directory. For windows operating system, go to TC\bin directory, you will see emp.txt file. It will have following information. • emp.txt • Id= 1 Name= sonoo Salary= 120000