















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Dr. Hanif Durad
^
^
Dr. Hanif Durad
D:\C Language\Hanif Search\Structures\Lecture 23 - Structs and Enumeration - 06.ppt
Does Not Reserve Space struct my_example{
int label;char letter;char name[20]; } ; /* The name "my_example" is
called a structure tag
Reserves Space struct my_example{
int label;char letter;char name[20]; } mystruct Dr. Hanif Durad
float gpa; } ; struct identity
//Create a second struct that includes the first one.
{ char name[30];
struct personal person; } ;
Dr. Hanif Durad
struct1.c
^
Individual members of a
struct
variable may be accessed using the
structure member operator (the dot, “.”):
mystruct.letter ;
^
Or , if a pointer to the
struct
has been declared and initialized
Some_name
*myptr = &mystruct ;
by using the structure pointer operator (the “->“):
myptr -> letter ;
which could also be written as:
(*myptr).letter ;
Dr. Hanif Durad
char phone[12];}; /* Declare an array of structures. */struct entry list[3];int i;
Dr. Hanif Durad
phone_struct.c
Teach Yourself in 21Days/c/ch11/ch11.htm
/* Print two blank lines. /printf("\n\n");/ Loop to display data. */for (i = 0; i < 3; i++){
printf("Name: %s %s", list[i].fname, list[i].lname);printf("\t\tPhone: %s\n", list[i].phone); } return 0; }
Dr. Hanif Durad
^
The C language provides a facility called
typedef
for creating
synonyms for previously defined data type names.
For
example, the declaration:
typedef int Length;
makes the name
Length
a synonym (or alias) for the data type
int. ^
The data “type” name
Length
can now be used in declarations in
exactly the same way that the data type
int
can be used:
Length
a, b, len ;
Length
numbers[10] ;
Dr. Hanif Durad
typedef struct t_name{ char last_name[25];char first_name[15];char middle_init[2];} NAME;...NAME my_name, your_name;... your_name = my_name; ... In the preceding example, the entire contents of the my_name structure were copied into the
your_name structure. This is essentially the same as the following line: memcpy(your_name, my_name, sizeof(your_name));
D:\C Language\C Programming - Just the FAQS – PDF\01 - The C Language.pdf
^
A union is an object that can hold any one of a set of namedmembers. ^
The members of the named set can be of any data type. ^
Members are overlaid in storage. ^
The storage allocated for a union is the storage required for thelargest member of the union, plus any padding required for theunion to end at a natural boundary of its strictest member. ^
Unions are typically used to save space, by not storing allpossibilities for certain data items that cannot occur together http://lectures-c.blogspot.com/2009/02/structures-and-union.html
Prentice Hall - Expert C Programming.pdf, P-
int x;double y; }; /* end union number */
Dr. Hanif Durad
union_examp.c
int main( void ){
union number value; /* define union variable /value.x = 100; / put an integer into the union */printf( "%s\n%s\n%s\n %d\n\n%s\n %f\n\n\n“, "Put a value in the integermember", "and print both members.",
"int:", value.x, "double:", value.y );value.y = 100.0; /* put a double into the same union / printf( "%s\n%s\n%s\n %d\n\n%s\n %f\n“, "Put a value in the floatingmember“, "and print both members.“, "int:", value.x, "double:", value.y );return 0; / indicates successful termination / } / end main */
Dr. Hanif Durad
{ int day1, day2, diff; //define variables
//of type days_of_week
day1 = Mon;
//give values to
day2 = Thu;
//variables
Dr. Hanif Durad
enum2.c
Modified fromRobert Lafore C++
//can do integer arithmetic
printf ("Days between = %d \n", diff);if(day1 < day2)
//can do comparisons
printf ("day1 comes before day2\n");return 0;}
Dr. Hanif Durad