Time: Year, Month, Day, Hour, Minutes, Second Complex Number: Real Part, Imaginary Part Point in A 2-Dimensional Plane: X Axis, y Axis
Time: Year, Month, Day, Hour, Minutes, Second Complex Number: Real Part, Imaginary Part Point in A 2-Dimensional Plane: X Axis, y Axis
• An aggregate data type for holding data of different types (int, char, float)
under one name
• A single struct would store the data for one object. An array of
structs would store the data for several objects.
struct pt3d
{
structure
int pt;
braces elements
double x, y, z;
surround
element };
declarations
semicolon required
Declaring Structures (struct)
variable
int main(void) name(s)
{
int i;
variable struct pt3d pt1, pt2; struct pt3d
type pt[5]; array
.... variable
return 0;
}
A Point Structure
/* ptstruc.c void main()
* A point structure example {
*/ a.x = 0; a.y = 0; // Origin, left-top point
#include <stdio.h> b.x = 24; b.y = 79;
PRINT(c.x, c.y);
}
Output of ptstruc,c
a.x = 0, a.y = 0
b.x = 24, b.y = 79
c.x = 12, c.y = 39
User Defined Data Types (typedef)
• 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] ;
// strucary.c
// Array of Structure aptr = COMPANIES;
#include <stdio.h> // get structure array's address for
#include <stdlib.h> // the structure pointer
#include "account.c" for(i = 0; i < N; i++)
#define N 2 {
struct account getinput(void); COMPANIES[i] =
// get input return a getinput();
// account structure putchar('\n');
void print(struct account);
print(*aptr++);
// print the whole structure
}
void main()
}
{
int i;
struct account COMPANY;
struct account *aptr;
// define a structure pointer
struct account COMPANIES[N];
// an array of structures for
// N customers
Self-Referential Structures
• Self-referential structures
– Structure that contains a pointer to a structure of the same type
– Can be linked together to form useful data structures such as lists, queues,
stacks and trees
– Terminated with a NULL pointer (0)
• Diagram of two self-referential structure objects linked together
15 10
Data member
NULL pointer (points to nothing)
and pointer
union Utype {
int ival;
float fval;
char *sval;
};
union Utype x, y, z;
• Accessing members of a union is via “.” member operator or, for pointers to
unions, the -> operator.
• A union holds the value of one-variable at a time.
• The compiler allocates storage for the biggest member of the union.
• The type retrieved from the union must be the type most recently stored. Otherwise,
the result is implementation dependent.
• union Utype x;
x.fval = 56.4; /* x holds type float. */
printf("%f\n", x.fval); /* OK. */
printf("%d\n", x.ival); /* Implementationdependent. */
struct modem{
unsigned ready: 1; // reserve 1 bit for ready
unsigned offhook: 2; // reserve 2 bits
unsigned reserved: 13; // 13-bit reserved
} com1;