Union |
|
Accessing a union member |
|
To access a union member, you can use the same syntax that you use for structure members. |
|
Example: |
|
code.m, code.x etc. |
|
During execution, we should make sure that the value of accessing member is currently stored. |
|
|
|
|
|
|
Union |
|
Initialization of Union variable |
|
A union variable can be initialized , provided its storage class is either external or static. |
|
Only one member of a union can be assigned a value at any one time. |
|
The initialization value is assigned to the first member within the union. |
|
An example program to demonstrate initialization of union variables: |
|
 |
|
|
|
|
Union |
|
Uses of Union |
|
Union, like structure, contain members whose individual data type may differ to each other. |
|
But the members that compose a union share the same storage area within the computer's memory, whereas each member within a structure is assigned its own unique storage area. |
|
Thus, union are used to conserve memory. |
|
1. Unions are useful for application involving multiple members, where value need not to be assigned to all of the members at any one time. |
|
2. Unions are useful whenever there is a requirement to access the same memory location in more than one way. etc. |
|
|
|
|
Dynamic Data Structure |
|
Linked list |
|
Before talking about the different mechanism of data structure we will take a short view ofDMA (Dynamic Memory Allocation). |
|
DMA: C language requires the number of elements in an array to be specified at compile time. |
|
But it is not practically possible with Array. |
|
In array we allocate the memory first and then start using it. |
|
This may result in failure of a program or wastage of memory space. |
|
The concept of dynamic memory allocation can be used to eradicate this problem. |
|
In this technique , the allocation of memory is done at run time. |
|
C language provides four library function known as memory management function that can be used for allocating and freeing memory during program execution. |
|
These are: |
|
malloc: allocate memory and return a pointer to the first byte of allocated space. |
|
Example: |
|
ptr=(cast.type*)malloc(byte_size); |
|
calloc: allocates the memory spaces, initialize them to zero and returns pointer to first byte. |
|
Example: |
|
ptr=(cast_type*)calloc(n.elem_size); |
|
free: frees previously allocated space. |
|
Example: |
|
free(ptr); |
|
realloc: modifies the size of previously assigned space |
|
Example: |
|
ptr=realloc(ptr,newsize); |
|
We studied about Array there we can observe one major disadvantage of Array is ,if an array is not filled by value, then memory will be locked up. |
|
To overcome this problem we use Linked lists and other data structure mechanism. |
|
Linked List are a way to store data with structures so that the programmer can automatically create a new place to store data whenever necessary. |
|
Specifically, the programmer writes a struct definition that contains variables holding information about something, and then has a pointer to a struct of its type. |
|
Each of these individual struct in the list is known as a node. |
|
Think of it like a train. The programmer always stores the first node of the list. This would be the engine of the train. |
|
The pointer is the connector between cars of the train. |
|
Every time the train add a car, it uses the connectors to add a new car. |
|
This is like a programmer using the keyword new to create a pointer to a new struct |
|
In memory it is often described as looking like this: |
|
---------- ---------- |
- Data - >- Data -> |
---------- - ---------- |
- Pointer- - - - Pointer- |
---------- ---------- |
|
|
|
|
Dynamic Data Structure |
|
Stack |
|
A stack is a data structure that resembles a stack of trays in a spring loaded bin. |
|
A tray will be added to the bin on top of the stack. When you add a tray, the previous one on top will go down by one position. |
|
You can add trays till the first trays reach the bottom of the stack. Similarly, a tray can be removed only from the top of the stack. |
|
In the computer science item is nothing but a data element or an object. |
|
Therefore a stack is a list in which items are added, deleted or examined at one only one end. |
|
The size of the stack is defined by the user before compilation and hence this is a static data structure. |
|
It adopts LIFO (last in first out ) methodology for storage and retrieval. |
|
|
|
|
Dynamic Data Structure |
|
Queue |
|
Queue is also a list. Here, the data items are added at one end and removed from the other hand work as first in first out for storage and retrieval. |
|
Queues are used extensively in operating systems to keep track of user waiting for resources such as CPU, printing etc. |
|
It adopts FIFO(first in first out )methodology for storage and retrieval. |
|
|
|
|
|
|
|
Data File Handling through C |
|
Introduction |
|
Many applications require that information be written to or read from an auxiliary memory. |
|
Such information is stored on the memory device in the form of a data file. |
|
Thus files allow us to store information permanently, and to access and alter that information whenever necessary. |
|
In C, an extensive set of library; functions is available for creating and processing data files. Unlike other files. |
|
However, there are two different types of data files |
|
1. Stream-oriented (or standard) data files: |
|
Stream oriented data files are generally easier to work with and are therefore more commonly used. |
|
Stream-oriented data files can be subdivided into two categories. |
|
The first category are text files, consisting of consecutive characters. There characters can be interpreted as individual data items, or as components of strings or numbers. |
|
2. System-oriented (or low level) data files: |
|
System-oriented data files, often referred to as unformatted data files, organizes structures, such as arrays and structures. |
|
A separate set of library functions is available for processing stream-oriented data files of this type. |
|
These library functions provide single instructions that can transfer entire arrays or structures to or from data files. |
|
Note: Library function has been discussed in detail in next section. |
|
|
|
|
Data File Handling through C |
|
File operation |
|
There are two distinct ways to perform the file operation in C: |
|
1. Low level I/O operation (it uses UNIX system call therefore we won't discuss here) |
|
2. High level I/O operation (it uses function in C's Standard I/O library) |
|
List of I/O function with their operation: |
|
fopen() | creates a new file for use or opens an existing file for use. |
fclose() | close a file which has been opened for use. |
getc() | reads a character to a file. |
putc() | writes a character to a line. |
fprintf() | writes a set of data values to a file. |
fscanf() | reads a set of data values from a file. |
getw() | reads an integer from a file. |
putw() | writes an integer to a file. |
feof() | test for an end of file condition. |
|
|
|
|
|
Data File Handling through C |
|
Opening & closing a data file |
|
When working with a stream-oriented data file, the first step is to establish a buffer area (a holt station for data processing) where information is temporarily stored while being transferred between the computer's memory and the data file. |
|
This buffer area allows information to be read from or written to the data file more readily than would otherwise be possible. The buffer area is established by writing |
|
FILE * ptvar; |
|
Where FILE (uppercase letter required) is a special structure type that establishes thebuffer area, and ptvar is a pointer variable that indicates the beginning of the buffer area. |
|
The structure type FILE is defined within a system include file, typically stdio.h. The pointerptvar is often referred to as a stream pointer, or simply a stream. |
|
A data file must be opened before it can be created or processed. This associates the file name with the buffer area (i.e., with the stream). |
|
It also specifies how the data file will be utilized, i.e., as a read-only file, a write -only file, or a read/write file, in which both operations are permitted. |
|
The library function open is used to open a file. This function is typically written as Ptvar =open (file-name, file-type) |
|
Where file-name and file-type are strings that represent the name of the data file and the manner in which the data file will be utilized. |
|
The name chosen for the file-name must be consistent with the rules for naming files, as determined by the computer's operating system. |
|
The file-type must be one of the strings shown: |
|
"r" | Open an existing file for reading only. |
"w" | Open a new file for writing only. If a file with the specified filename currently exists, it will be destroyed and a new file will be created in its place. |
"a" | Open an existing file for appending. A new file will be created if the file with the specified file-name don't exists. |
"r+" | open an existing file for both reading and writing |
"w+" | Open an existing file for both reading and writing. If a file with the specified file name currently exists, it will be destroyed and a new file created in its place |
"a+" | Open an existing file for both reading and appending. A new file will be created if the file with the specified file-name does not exists. |
|
|
|
|
|
Data File Handling through C |
|
Creating a data file |
|
A data file must be created before it can be processed. |
|
A stream-oriented data file can be created in two ways. One is to create the file directly, using a text editor or a word processor. |
|
The other is to write a program that enters information into the computer and than writes it out to the data file. |
|
Unformatted data files can only be created with such specially written programs. |
|
Example: |
|
Reading a data file- |
|
The following program will read a line of text from a data file on a character by character basis, and display the text on the screen. |
|
The program makes use of the library functions get and putchar to read and display the data. |
|
An example program to read a line of text from a data file and display it on the screen: |
|
 |
|
Data file consisting entirely of strings can often be created and read more casually with programs that utilize special string-oriented library functions. |
|
Some commonly used functions of this type are gets, puts, fgets and fputs. |
|
The functions gets and puts read or write strings to or from the standard output devices, whereas fgets and fputs exchange strings with data files. |
|
|
|
|
Data File Handling through C |
|
Processing a data file |
|
Most data file applications require that a data file be altered as it is being processed. |
|
For example, in an application involving the processing of customer records, it may be desirable to add new records to the file there requirements in turn suggest several different computational strategies. |
|
Another approach is to work with two different data files- an old file and a new file. Each record is read from the old file, updated as necessary, and then written to the new file. |
|
When all of the records have been updated, the old file is deleted or placed into archival storage and the new file renamed. Hence, the new file become the struck for the next round of updates. |
|
Historically, the origin of this method goes back to the early days of computing, when data files were maintained on magnetic tapes. The method is still used, however, because it provides a series of old source. |
|
File that can be used to generate a customer history. The most recent source file can also be used to recreate the current file if the current file is damaged or destroyed. |
|
|
|
|
Data File Handling through C |
|
Unformatted data file |
|
Some applications involve the use of data files to store block of data, where each block consists of a fixed number of contiguous bytes. |
|
Each block will generally represent a complex data structure, such as a structure or an array. |
|
For example, a data file may consist of multiple structures having the same composition, or it may contain multiple arrays of the same type and size. |
|
For such applicators it may be desirable to read the entire block from the data, or write the entire block to the data file, rather than reading or writing the individual components (i.e., structure members of array elements) within each block separately. |
|
The library functions fread and fwrite are intended to be used in situations of this type. |
|
There functions are often referred to as unformatted read and write functions. Similarly, data files of this type are often referred to as unformatted data file. |
|
Each of these functions requires four arguments: a pointer to the data block, the size of the data block, the number of data blocks being transferred, and the stream pointer. |
|
Thus, a typical fwrite function might be written as: fwrite(&customer, sizeof(record), 1, fpt); |
|
Where customer is a structure variable of type record, and fpt is the stream pointer associated with a data file that has been opened for output. |
|
An example program to create an unformatted data file containing customer records: |
|
 |
|
Reading file: |
|
Displaying the contents: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
No comments:
Post a Comment