by Dinesh Thakur Category: Structure And Union

As we already know, a structure is an ordered list of elements of either the same or different types. A union is similar to a structure but with one major difference that the structure stores all its members one after another, whereas the union can store only one member at a time, since all the members in a union are stored beginning from the same memory location. Thus, the total memory required to store a union is the same as that required for the largest member in it. The union is useful when we have to store one of the alternative values of different types and yet conserve space.

 
by Dinesh Thakur Category: Structure And Union

The program given below reads the information about a person and prints it on the screen. It uses a structure containing a pointer member name to represent the information about a person. Note that the program uses the dstr_read function.



 
by Dinesh Thakur Category: Structure And Union

A program to determine the number of words and average word length is given in Program. It uses the strtok function to separate the words in a given string. The program given below uses the same technique to separate the words in a given string and determine and print the frequency of these words.

 
by Dinesh Thakur Category: Structure And Union

The C language permits structures to be nested, in which a structure can contain structure members. A structure may be declared inside another structure. Also, a structure may be a data member of another structure. The important thing to note is how to initialize the data members of inner structures and how to access the data members of inner structures. The use of such nested structures facilitates representation and processing of complex data.

 
by Dinesh Thakur Category: Structure And Union

The C language allows an array to be used as a structure member. This enables us to group related structure members and is also useful for declaring character strings as structure members. This section explains how we can effectively use such structures. This includes

 
by Dinesh Thakur Category: Structure And Union

Consider the problem of comparison of two valid dates d1 and d2. There are three possible outcomes of this comparison: d1 == d2 (dates are equal), d1 > d2 (date d1 is greater, i.e., occurs after d2) and d1 < d2(date d1 is smaller, i.e., occurs before d2). Let us write a function that accepts two dates as structures dl and d2 of type struct date and returns 0 if the dates are equal, 1 if d1 is later than d2 and -1 if date dl is earlier than d2.

 
by Dinesh Thakur Category: Structure And Union

Let us use structure complex to represent a complex number and write a simple program containing only the main function. Thus, we can define structure complex either in the main function or before it as a global definition.

 
by Dinesh Thakur Category: Structure And Union

Structures and Unions, Giving values to members, Initializing structure, Functions and structures, Passing structure to elements to functions, Passing entire function to functions, Arrays of structure, Structure within a structure and Union.

 
by Dinesh Thakur Category: Structure And Union

Union is a collection of data items of different data types. It can hold data of only one member at a time though it has members of different data types. If a union has two members of different data types, they are allocated the same memory.



 
by Dinesh Thakur Category: Structure And Union

A structure is a user-defined data type containing a collection of logically related data which may be of different types such as int, double, char, and so on. All of them are encapsulated (packed) in a single unit with a single name. The classes of C++ are in fact generalization of C-structures. A class in C++ with only public members is similar to a C-structure. All members of a structure in C are by default public. However, in a class declaration in C++, if there is no access specifier, the members are private by default. We know that arrays can be used to represent a group of data items that belong to the same type, such as int or float. This restriction is not there in structures.

 
by Dinesh Thakur Category: File Handling

Function clearerr ()

The function prototype is as given below.

void(clearerr(FILE* Stream);

 
by Dinesh Thakur Category: File Handling

Function putw()

This function writes an integer (whole number) into a file. The prototype of the function is as shown below.

 
by Dinesh Thakur Category: File Handling

Appending to a file means adding at the end of the file while keeping the previous contents of the file intact. For this the file open mode is "a". Program illustrates appending a file.

 
by Dinesh Thakur Category: File Handling

The function prototype is written in the following manner:

int ungetc(int c, File *stream);

 
by Dinesh Thakur Category: File Handling

The prototype of this function is given below.

int remove(const char* filename);

 
by Dinesh Thakur Category: File Handling

This function is used to change the name of file. Its prototype may be written as given below.

 
by Dinesh Thakur Category: File Handling

This function may be used for opening a file in different modes. The function prototype may be written in the following manner:

 
by Dinesh Thakur Category: File Handling

To read a string in a file we can use fgets () whose prototype is:

char * fgets (char * str, int size, FILE * fp);

 
by Dinesh Thakur Category: File Handling

Let us define a structure student to store the HSC marks data of a student as follows:

 
by Dinesh Thakur Category: File Handling

The fscanf () function works as scanf (). The difference is that fscanf () reads a file and not the computer keyboard. prototype:

int fscanf (FILE * fp, char * str, ...);



 

Page 14 of 16



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.