Structures and Unions
description
Transcript of Structures and Unions
![Page 1: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/1.jpg)
Structures and Unions
Chapter 6
![Page 2: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/2.jpg)
Structure
A structure is an aggregate data type Composed of two or more related variables called
member/field/element
struct tag-name{type member1;type member2;type member3;.type memberN;
} variable-list;
![Page 3: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/3.jpg)
Structure Contd.
struct point{int x;int y;
}; Tag-name and variable-list are optional but one of them must
present Structure can be initialized
strcut point pt = {20, 30}; Member can be accessed via . operator
struct point pt;pt.x = 20;pt.y = 30;
![Page 4: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/4.jpg)
Array of Structures
stuct student{
long id;
char name[20];
char dept;
};
struct student s[100];
![Page 5: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/5.jpg)
Array of Structures Contd.
stuct {int id;char name[20];char dept[4];
} s[100];
scanf(“%d”, &s[0].id);scanf(“%s”, s[0].name);scanf(“%s”, s[0].dept);
![Page 6: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/6.jpg)
Nested Structures
struct careof{char houseno[20];int roadno;char location[80];char phone[20];
};struct student_info{
int id;char name[80];struct careof address;
}s;
![Page 7: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/7.jpg)
Nested Structures Contd.
Accessing the nested structure
scanf(“%s”, s.address.houseno);
scanf(“%d”, &s.address.roadno);
![Page 8: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/8.jpg)
Union
union is a single piece of memory that is shared by two or more variables
union tag-name{type member1;type member2;type member3;.type memberN;
} variable-list;
![Page 9: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/9.jpg)
Union Contd.
union u_type{
int i;
char c[2];
float d;
}sample;
1 2 3 4
d
c[0] c[1]
i
![Page 10: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/10.jpg)
Suppose that a constant may be an int, a float, or a charstruct {
char name[20];
int utype;
union u_tag{
int ival;
float fval;
char cval;
}u;
}symtab[MAX];
![Page 11: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/11.jpg)
Structure: Bit-fields
So far we cannot access at bit level Bit-fields are useful when you want to pack
information into the smallest possible space
struct b_type{
unsigned dept: 3;
unsigned stock: 2;
}var_list;
![Page 12: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/12.jpg)
Structure: Bit-fields Contd.
The members are all either integer or unsigned integer For integer the left most bit will be regarded as sign bit
Can be assigned values confirming its limit A field may overlap a word boundary is
implementation-defined Fields may not be named (: and width) used for
padding Processor architecture dependent
![Page 13: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/13.jpg)
File I/O
Chapter 9
Teach Yourself
by Herbert Schildt
![Page 14: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/14.jpg)
Understanding Streams
Stream: the C I/O system supplies a consistent interface to the programmer for device I/O files A level of abstraction A logical interface
File: actual device providing I/O is called a file
![Page 15: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/15.jpg)
Standard Streams
stdin stdout stderr
![Page 16: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/16.jpg)
Types of Stream
Two types Text Binary
Text file Contains ASCII characters Some character translation So, no one-to-one correspondence between what is sent to
the stream and what is written to the file Binary file
May be used with any type of data No character translation So there is one-to-one correspondence
![Page 17: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/17.jpg)
How to open a file?
FILE *fopen (char *filename, char *mode); Stdio.h Filename
path Mode
“r”, “w”, “a”, “rb”, “ab”, “r+” Consequence of those modes
![Page 18: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/18.jpg)
How to close a file?
Int fclose (FILE *fp); In order to improve efficiency most file
system write data to disk one sector at a time Fclose flushes the buffer
![Page 19: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/19.jpg)
How to know it is the end of file?Int feof (FILE *fp);
Int ferror (FILE *fp);
![Page 20: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/20.jpg)
How to read and write a character?Int fgetc (FILE *fp);
Int fputc (int ch, FILE *fp);
And more on this later
![Page 21: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/21.jpg)
An example: reading a text file and displaying it in the screenFILE *fp;
if ((fp = fopen (“a.txt”, “r”))==NULL){
//error and exit
}
while (! feof (fp)){
putchar (fgetc(fp));
}
fclose (fp);
![Page 22: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/22.jpg)
Writing and reading strings and othersInt fputs (char *str, FILE *fp);
Int fgets (char *str, int num, FILE *fp);
Int fprintf (FILE *fp, format speci, variable(s));
Int fscanf (FILE *fp, format speci, address(es));
![Page 23: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/23.jpg)
How to read/write in binary mode?size_t fread (void *buffer, size_t size, size_t num, FILE
*fp);
size_t fwrite (void *buffer, size_t size, size_t num, FILE *fp);
size_t: defined in stdio.h
unsigned long
Void pointer: pointer of any data types
![Page 24: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/24.jpg)
An example: reading a text file and displaying it in the screenFILE *fp; char ch;if ((fp = fopen (“a.txt”, “rb”))==NULL){
//error and exit}while (! feof (fp)){
fread(&ch, sizeof (char), 1);putchar (ch);
}fclose (fp);
![Page 25: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/25.jpg)
Writing an entire array
double d[10] = {10.2, 20.3,….};
fwrite (d, sizeof d, 1, fp); //entire
fread (d, sizeof (double), 5, fp); //only first five elements
![Page 26: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/26.jpg)
Random Access
So far, we have seen write and read sequentially Beginning to end
Using another function we can access any point in a file Used only in binary mode (one-to-one)
int fseek (FILE *fp, long offset, int origin);
![Page 27: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/27.jpg)
Origins are
SEEK_SET //seek from the start of file SEEK_CUR //seek from current location SEEK_END //seek from end of file
long ftell (FILE *fp);
![Page 28: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/28.jpg)
Example: copy a file into another in reverse orderFILE *in, *out; char ch; long loc;fseek(in, 0L, SEEK_END);loc = ftell(in);loc = loc -1; //skip the end markerwhile (loc >= 0){
fseek(in, loc, SEEK_SET);ch = fgetc(in);fputc(ch, out);loc--;
}
![Page 29: Structures and Unions](https://reader036.fdocuments.in/reader036/viewer/2022062305/56814dcc550346895dbb2353/html5/thumbnails/29.jpg)
Example??
10 double numbers are written in a file User want to access any number