C – Unit V

UNIT 5: POINTERS AND FILES

 

5.1 POINTERS

 

Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So, it becomes necessary to learn pointers to become a perfect C programmer. Let’s start learning them in simple and easy steps.

 

As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following example, which prints the address of the variables defined –

 

#include <stdio.h>

 

int main () {

 

int  var1;

char var2[10];

 

printf(“Address of var1 variable: %x\n”, &var1  );

printf(“Address of var2 variable: %x\n”, &var2  );

 

return 0;

}

 

When the above code is compiled and executed, it produces the following result −

Address of var1 variable: bff5a400

Address of var2 variable: bff5a3f6

 

 

 

 

What are Pointers?

 

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −

type *var-name;

 

Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations –

 

int    *ip;    /* pointer to an integer */

double *dp;    /* pointer to a double */

float  *fp;    /* pointer to a float */

char   *ch     /* pointer to a character */

 

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

 

5.2 DECLARATIONS

 

There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. The following example makes use of these operations −

 

#include <stdio.h>

 

int main () {

 

int  var = 20;   /* actual variable declaration */

int  *ip;        /* pointer variable declaration */

 

ip = &var;  /* store address of var in pointer variable*/

 

printf(“Address of var variable: %x\n”, &var  );

 

/* address stored in pointer variable */

printf(“Address stored in ip variable: %x\n”, ip );

 

/* access the value using the pointer */

printf(“Value of *ip variable: %d\n”, *ip );

 

return 0;

}

 

When the above code is compiled and executed, it produces the following result −

Address of var variable: bffd8b3c

Address stored in ip variable: bffd8b3c

Value of *ip variable: 20

 

NULL Pointers

 

It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

 

The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program −

 

#include <stdio.h>

 

int main () {

 

int  *ptr = NULL;

 

printf(“The value of ptr is : %x\n”, ptr  );

 

return 0;

}

 

When the above code is compiled and executed, it produces the following result −

The value of ptr is 0

 

In most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.

 

To check for a null pointer, you can use an ‘if’ statement as follows −

if(ptr)     /* succeeds if p is not null */

if(!ptr)    /* succeeds if p is null */

 

Pointers in Detail

 

Pointers have many but easy concepts and they are very important to C programming. The following important pointer concepts should be clear to any C programmer –

 

Sr.No. Concept & Description
1 Pointer arithmetic

There are four arithmetic operators that can be used in pointers: ++, –, +, –

2 Array of pointers

You can define arrays to hold a number of pointers.

3 Pointer to pointer

C allows you to have pointer on a pointer and so on.

4 Passing pointers to functions in C

Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function.

5 Return pointer from functions in C

C allows a function to return a pointer to the local variable, static variable, and dynamically allocated memory as well.

 

5.3 PASSING POINTERS TO FUNCTIONS

 

C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.

 

Following is a simple example where we pass an unsigned long pointer to a function and change the value inside the function which reflects back in the calling function –

 

#include <stdio.h>

#include <time.h>

 

void getSeconds(unsigned long *par);

 

int main () {

 

unsigned long sec;

getSeconds( &sec );

 

/* print the actual value */

printf(“Number of seconds: %ld\n”, sec );

 

return 0;

}

 

void getSeconds(unsigned long *par) {

/* get the current number of seconds */

*par = time( NULL );

return;

}

 

When the above code is compiled and executed, it produces the following result −

Number of seconds :1294450468

 

The function, which can accept a pointer, can also accept an array as shown in the following example −

 

#include <stdio.h>

 

/* function declaration */

double getAverage(int *arr, int size);

 

int main () {

 

/* an int array with 5 elements */

int balance[5] = {1000, 2, 3, 17, 50};

double avg;

 

/* pass pointer to the array as an argument */

avg = getAverage( balance, 5 ) ;

 

/* output the returned value  */

printf(“Average value is: %f\n”, avg );

return 0;

}

 

double getAverage(int *arr, int size) {

 

int  i, sum = 0;

double avg;

 

for (i = 0; i < size; ++i) {

sum += arr[i];

}

 

avg = (double)sum / size;

return avg;

}

 

When the above code is compiled together and executed, it produces the following result −

Average value is: 214.40000

 

5.4 OPERATION IN POINTERS

 

A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value. There are four arithmetic operators that can be used on pointers: ++, –, +, and –

 

To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer −

ptr++

 

After the above operation, the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer location which is 4 bytes next to the current location. This operation will move the pointer to the next memory location without impacting the actual value at the memory location. If ptr points to a character whose address is 1000, then the above operation will point to the location 1001 because the next character will be available at 1001.

 

 

 

Incrementing a Pointer

 

We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer. The following program increments the variable pointer to access each succeeding element of the array −

 

#include <stdio.h>

 

const int MAX = 3;

 

int main () {

 

int  var[] = {10, 100, 200};

int  i, *ptr;

 

/* let us have array address in pointer */

ptr = var;

 

for ( i = 0; i < MAX; i++) {

 

printf(“Address of var[%d] = %x\n”, i, ptr );

printf(“Value of var[%d] = %d\n”, i, *ptr );

 

/* move to the next location */

ptr++;

}

 

return 0;

}

 

When the above code is compiled and executed, it produces the following result −

Address of var[0] = bf882b30

Value of var[0] = 10

Address of var[1] = bf882b34

Value of var[1] = 100

Address of var[2] = bf882b38

Value of var[2] = 200

 

Decrementing a Pointer

 

The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data type as shown below −

 

#include <stdio.h>

 

const int MAX = 3;

 

int main () {

 

int  var[] = {10, 100, 200};

int  i, *ptr;

 

/* let us have array address in pointer */

ptr = &var[MAX-1];

 

for ( i = MAX; i > 0; i–) {

 

printf(“Address of var[%d] = %x\n”, i-1, ptr );

printf(“Value of var[%d] = %d\n”, i-1, *ptr );

 

/* move to the previous location */

ptr–;

}

 

return 0;

}

 

When the above code is compiled and executed, it produces the following result −

Address of var[2] = bfedbcd8

Value of var[2] = 200

Address of var[1] = bfedbcd4

Value of var[1] = 100

Address of var[0] = bfedbcd0

Value of var[0] = 10

 

Pointer Comparisons

 

Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared.

 

The following program modifies the previous example − one by incrementing the variable pointer so long as the address to which it points is either less than or equal to the address of the last element of the array, which is &var[MAX – 1] −

 

#include <stdio.h>

 

const int MAX = 3;

 

int main () {

 

int  var[] = {10, 100, 200};

int  i, *ptr;

 

/* let us have address of the first element in pointer */

ptr = var;

i = 0;

 

while ( ptr <= &var[MAX – 1] ) {

 

printf(“Address of var[%d] = %x\n”, i, ptr );

printf(“Value of var[%d] = %d\n”, i, *ptr );

 

/* point to the previous location */

ptr++;

i++;

}

 

return 0;

}

 

When the above code is compiled and executed, it produces the following result −

Address of var[0] = bfdbcb20

Value of var[0] = 10

Address of var[1] = bfdbcb24

Value of var[1] = 100

Address of var[2] = bfdbcb28

Value of var[2] = 200

 

5.5 POINTERS AND ARRAYS

 

Arrays are closely related to pointers in C programming but the important difference between them is that, a pointer variable takes different addresses as value whereas, in case of array it is fixed.

 

This can be demonstrated by an example:

 

#include <stdio.h>

int main()

{

char charArr[4];

int i;

 

for(i = 0; i < 4; ++i)

{

printf(“Address of charArr[%d] = %u\n”, i, &charArr[i]);

}

 

return 0;

}

 

When you run the program, the output will be:

Address of charArr[0] = 28ff44

Address of charArr[1] = 28ff45

Address of charArr[2] = 28ff46

Address of charArr[3] = 28ff47

 

Note: You may get different address of an array.

 

Notice, that there is an equal difference (difference of 1 byte) between any two consecutive elements of array charArr.

 

But, since pointers just point at the location of another variable, it can store any address.

 

Relation between Arrays and Pointers

 

Consider an array:

int arr[4];

 

In C programming, name of the array always points to address of the first element of an array.

In the above example, arr and &arr[0] points to the address of the first element.

&arr[0] is equivalent to arr

 

Since, the addresses of both are the same, the values of arr and &arr[0] are also the same.

arr[0] is equivalent to *arr (value of an address of the pointer)

 

Similarly,

&arr[1] is equivalent to (arr + 1) AND, arr[1] is equivalent to *(arr + 1).

&arr[2] is equivalent to (arr + 2) AND, arr[2] is equivalent to *(arr + 2).

&arr[3] is equivalent to (arr + 3) AND, arr[3] is equivalent to *(arr + 3).

.

.

&arr[i] is equivalent to (arr + i) AND, arr[i] is equivalent to *(arr + i).

In C, you can declare an array and can use pointer to alter the data of an array.

 

Example: Program to find the sum of six numbers with arrays and pointers

 

#include <stdio.h>

int main()

{

int i, classes[6],sum = 0;

printf(“Enter 6 numbers:\n”);

for(i = 0; i < 6; ++i)

{

// (classes + i) is equivalent to &classes[i]

scanf(“%d”,(classes + i));

 

// *(classes + i) is equivalent to classes[i]

sum += *(classes + i);

}

printf(“Sum = %d”, sum);

return 0;

}

Output

 

Enter 6 numbers:

2

3

4

5

3

4

Sum = 21

 

5.6 ARRAYS OF POINTERS

 

An array of pointers is an indexed set of variables in which the variables are pointers (a reference to a location in memory).

 

Pointers are an important tool in computer science for creating, using, and destroying all types of data structures. An array of pointers is useful for the same reason that all arrays are useful: it allows you to numerically index a large set of variables.

 

Below is an array of pointers in C that sets each pointer in one array to point to an integer in another and then print the values of the integers by dereferencing the pointers. In other words, this code prints the value in memory of where the pointers point.

 

#include <stdio.h>

 

const int ARRAY_SIZE = 5;

 

int main ()

{

/* first, declare and set an array of five integers:    */

int array_of_integers[] = {5, 10, 20, 40, 80};

 

/* next, declare an array of five pointers-to-integers: */

int i, *array_of_pointers[ARRAY_SIZE];

 

for ( i = 0; i < ARRAY_SIZE; i++)

{

/* for indices 1 through 5, set a pointer to

point to a corresponding integer:                 */

array_of_pointers[i] = &array_of_integers[i];

}

 

for ( i = 0; i < ARRAY_SIZE; i++)

{

/* print the values of the integers pointed to

by the pointers:                                  */

printf(“array_of_integers[%d] = %d\n”, i, *array_of_pointers[i] );

}

 

return 0;

}

 

The output of the above program is:

 

array_of_integers[0] = 5

array_of_integers[1] = 10

array_of_integers[2] = 20

array_of_integers[3] = 40

array_of_integers[4] = 80

 

5.7 STRUCTURES AND POINTERS

 

Structures can be created and accessed using pointers. A pointer variable of a structure can be created as below:

 

struct name {

member1;

member2;

.

.

};

 

int main()

{

struct name *ptr;

}

Here, the pointer variable of type struct name is created.

 

Accessing structure’s member through pointer

 

A structure’s member can be accesssed through pointer in two ways:

  1. Referencing pointer to another address to access memory
  2. Using dynamic memory allocation

 

  1. Referencing pointer to another address to access the memory

 

Consider an example to access structure’s member through pointer.

 

#include <stdio.h>

typedef struct person

{

int age;

float weight;

};

 

int main()

{

struct person *personPtr, person1;

personPtr = &person1;            // Referencing pointer to memory address of person1

 

printf(“Enter integer: “);

scanf(“%d”,&(*personPtr).age);

 

printf(“Enter number: “);

scanf(“%f”,&(*personPtr).weight);

 

printf(“Displaying: “);

printf(“%d%f”,(*personPtr).age,(*personPtr).weight);

 

return 0;

}

 

In this example, the pointer variable of type struct person is referenced to the address of person1. Then, only the structure member through pointer can can accessed.

 

Using -> operator to access structure pointer member

 

Structure pointer member can also be accessed using -> operator.

(*personPtr).age is same as personPtr->age

(*personPtr).weight is same as personPtr->weight

 

  1. Accessing structure member through pointer using dynamic memory allocation

 

To access structure member using pointers, memory can be allocated dynamically using malloc() function defined under “stdlib.h” header file.

 

Syntax to use malloc()

ptr = (cast-type*) malloc(byte-size)

 

 

 

Example to use structure’s member through pointer using malloc() function.

 

#include <stdio.h>

#include <stdlib.h>

struct person {

int age;

float weight;

char name[30];

};

 

int main()

{

struct person *ptr;

int i, num;

 

printf(“Enter number of persons: “);

scanf(“%d”, &num);

 

ptr = (struct person*) malloc(num * sizeof(struct person));

// Above statement allocates the memory for n structures with pointer personPtr pointing to base address */

 

for(i = 0; i < num; ++i)

{

printf(“Enter name, age and weight of the person respectively:\n”);

scanf(“%s%d%f”, &(ptr+i)->name, &(ptr+i)->age, &(ptr+i)->weight);

}

 

printf(“Displaying Infromation:\n”);

for(i = 0; i < num; ++i)

printf(“%s\t%d\t%.2f\n”, (ptr+i)->name, (ptr+i)->age, (ptr+i)->weight);

 

return 0;

}

 

Output

 

Enter number of persons: 2

Enter name, age and weight of the person respectively:

Adam

2

3.2

Enter name, age and weight of the person respectively:

Eve

6

2.3

Displaying Information:

Adam  2          3.20

Eve      6          2.30

 

5.8 FILES

 

A file represents a sequence of bytes, regardless of it being a text file or a binary file. C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices. This chapter will take you through the important calls for file management.

 

  • 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 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 that you can easily create using Notepad or any simple text editors.

 

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 least security and takes bigger storage space.

 

  1. 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 higher amount of data, are not readable easily and provides a better security than text files.

 

File Operations

 

In C, you can perform four major operations on the file, 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 program.

FILE *fptr;

 

5.9 CREATING

 

For reading and writing to a text file, we use the functions fprintf() and fscanf().

 

They are just the file versions of printf() and scanf(). The only difference is that, fprint and fscanf expects a pointer to the structure FILE.

 

Writing to a text file

 

Write to a text file using fprintf()

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

int num;

FILE *fptr;

fptr = fopen(“C:\\program.txt”,”w”);

 

if(fptr == NULL)

{

printf(“Error!”);

exit(1);

}

 

printf(“Enter num: “);

scanf(“%d”,&num);

 

fprintf(fptr,”%d”,num);

fclose(fptr);

 

return 0;

}

 

This program takes a number from user and stores in the file program.txt.

 

After you compile and run this program, you can see a text file program.txt created in C drive of your computer. When you open the file, you can see the integer you entered.

 

Writing to a binary file

 

To write into a binary file, you need to use the function fwrite(). The functions takes four arguments: Address of data to be written in disk, Size of data to be written in disk, number of such type of data and pointer to the file where you want to write.

fwrite(address_data,size_data,numbers_data,pointer_to_file);

 

Writing to a binary file using fwrite()

 

#include <stdio.h>

#include <stdlib.h>

 

struct threeNum

{

int n1, n2, n3;

};

 

int main()

{

int n;

struct threeNum num;

FILE *fptr;

 

if ((fptr = fopen(“C:\\program.bin”,”wb”)) == NULL){

printf(“Error! opening file”);

 

// Program exits if the file pointer returns NULL.

exit(1);

}

 

for(n = 1; n < 5; ++n)

{

num.n1 = n;

num.n2 = 5*n;

num.n3 = 5*n + 1;

fwrite(&num, sizeof(struct threeNum), 1, fptr);

}

fclose(fptr);

 

return 0;

}

 

In this program, you create a new file program.bin in the C drive.

We declare a structure threeNum with three numbers – n1, n2 and n3, and define it in the main function as num.

 

Now, inside the for loop, we store the value into the file using fwrite.

The first parameter takes the address of num and the second parameter takes the size of the structure threeNum.

 

Since, we’re only inserting one instance of num, the third parameter is 1. And, the last parameter *fptr points to the file we’re storing the data.

 

Finally, we close the file.

5.10 PROCESSING

 

Reading from a text file

 

Read from a text file using fscanf()

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

int num;

FILE *fptr;

 

if ((fptr = fopen(“C:\\program.txt”,”r”)) == NULL){

printf(“Error! opening file”);

 

// Program exits if the file pointer returns NULL.

exit(1);

}

 

fscanf(fptr,”%d”, &num);

 

printf(“Value of n=%d”, num);

fclose(fptr);

 

return 0;

}

This program reads the integer present in the program.txt file and prints it onto the screen.

If you succesfully created the file, running this program will get you the integer you entered.

 

Other functions like fgetchar(), fputc() etc. can be used in similar way.

 

 

Reading from a binary file

 

Function fread() also take 4 arguments similar to fwrite() function as above.

fread(address_data,size_data,numbers_data,pointer_to_file);

 

Reading from a binary file using fread()

 

#include <stdio.h>

#include <stdlib.h>

 

struct threeNum

{

int n1, n2, n3;

};

 

int main()

{

int n;

struct threeNum num;

FILE *fptr;

 

if ((fptr = fopen(“C:\\program.bin”,”rb”)) == NULL){

printf(“Error! opening file”);

 

// Program exits if the file pointer returns NULL.

exit(1);

}

 

for(n = 1; n < 5; ++n)

{

fread(&num, sizeof(struct threeNum), 1, fptr);

printf(“n1: %d\tn2: %d\tn3: %d”, num.n1, num.n2, num.n3);

}

fclose(fptr);

 

return 0;

}

 

In this program, you read the same file program.bin and loop through the records one by one.

In simple terms, you read one threeNum record of threeNum size from the file pointed by *fptr into the structure num.

 

5.11 OPENING AND CLOSING A DATA FILE

 

Opening a file is performed using the library function in the “stdio.h” header file: fopen().

The syntax for opening a file in standard I/O is:

 

ptr = fopen(“fileopen”,”mode”)

 

For Example:

 

fopen(“E:\\cprogram\\newprogram.txt”,”w”);

fopen(“E:\\cprogram\\oldprogram.bin”,”rb”);

 

  • Let’s suppose the file newprogram.txt doesn’t exist in the location E:\cprogram. The first function creates a new file named newprogram.txt and opens it for writing as per the mode ‘w’. The writing mode allows you to create and edit (overwrite) the contents of the file.
  • Now let’s suppose the second binary file oldprogram.bin exists in the location E:\cprogram. The second function opens the existing file for reading in binary mode ‘rb’. The reading mode only allows you to read the file, you cannot write into the file.

 

Opening Modes in Standard I/O
File Mode Meaning of Mode During Inexistence of file
r Open for reading. If the file does not exist, fopen() returns NULL.
rb Open for reading in binary mode. If the file does not exist, fopen() returns NULL.
w Open for writing. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
wb Open for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
a Open for append. i.e, Data is added to end of file. If the file does not exists, it will be created.
ab Open for append in binary mode. i.e, Data is added to end of file. If the file does not exists, it will be created.
r+ Open for both reading and writing. If the file does not exist, fopen() returns NULL.
rb+ Open for both reading and writing in binary mode. If the file does not exist, fopen() returns NULL.
w+ Open for both reading and writing. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
wb+ Open for both reading and writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
a+ Open for both reading and appending. If the file does not exists, it will be created.
ab+ Open for both reading and appending in binary mode. If the file does not exists, it will be created.

 

 

Closing a File

 

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

 

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

 

fclose(fptr); //fptr is the file pointer associated with file to be closed.

Write a C program to read name and marks of n number of students from user and store them in a file.

 

#include <stdio.h>

int main()

{

char name[50];

int marks, i, num;

 

printf(“Enter number of students: “);

scanf(“%d”, &num);

 

FILE *fptr;

fptr = (fopen(“C:\\student.txt”, “w”));

if(fptr == NULL)

{

printf(“Error!”);

exit(1);

}

 

for(i = 0; i < num; ++i)

{

printf(“For student%d\nEnter name: “, i+1);

scanf(“%s”, name);

 

printf(“Enter marks: “);

scanf(“%d”, &marks);

 

fprintf(fptr,”\nName: %s \nMarks=%d \n”, name, marks);

}

 

fclose(fptr);

return 0;

}

 

Write a C program to read name and marks of n number of students from user and store them in a file. If the file previously exits, add the information of n students.

 

#include <stdio.h>

int main()

{

char name[50];

int marks, i, num;

 

printf(“Enter number of students: “);

scanf(“%d”, &num);

 

FILE *fptr;

fptr = (fopen(“C:\\student.txt”, “a”));

if(fptr == NULL)

{

printf(“Error!”);

exit(1);

}

 

for(i = 0; i < num; ++i)

{

printf(“For student%d\nEnter name: “, i+1);

scanf(“%s”, name);

 

printf(“Enter marks: “);

scanf(“%d”, &marks);

 

fprintf(fptr,”\nName: %s \nMarks=%d \n”, name, marks);

}

 

fclose(fptr);

return 0;

}