Objectives of this lecture
How to declare and initialize structures
E.g: struct user_record {int id_no;
STRING name;
STRING dept;
int no_of_books;
USER_STATUS status;
};
struct
user_record user;
typedef
struct { int id_no;
STRING name;
STRING dept;
int no_of_books;
USER_STATUS status;
} USER_RECORD;
USER_RECORD
user;
#include
<stdio.h>
#include
<string.h>
typedef
char STRING[81];
typedef
enum {STUDENT, STAFF, FACULTY} USER_STATUS;
typedef
struct {int id_no;
STRING name;
STRING dept;
int no_of_books;
USER_STATUS status;
} USER_RECORD;
main()
{ USER_RECORD user;
user.no_of_books=0;
printf("Enter user details:\n");
printf("ID_NO: "); scanf("%d",&user.id_no);
printf("NAME: "); fflush(stdin); gets(user.name);
printf("DEPARTMENT: "); gets(user.dept);
printf("STATUS [0 for student, 1 for
staff or 2 for faculty: ");
scanf("%d",&user.status);
printf("\nThe details you entered
are:\n");
printf("ID_NO: %d\n", user.id_no);
printf("NAME: %s\n", user.name);
printf("DEPARTMENT: %s\n",
user.dept);
printf("STATUS: %d\n", user.status);
printf("NO OF BOOKS %d\n",
user.no_of_books);
return 0;
}
Nested (or compound) Structure:
E.g. typedef struct { int day;
int month;
int year;
}
DATE;
typedef struct { int bk_no;
STRING author;
STRING title;
STRING
publisher;
DATE pub_date;
BOOK_STATUS status;
int edition
}
BOOK_RECORD;
BOOK_RECORD book;
book.pub_date.day=2;
book.pub_date.month=5;
book.pub_date.year=1980;
Array of structures:
BOOK_RECORD
books[100];
Books[0].bk_no=3333;
Assignment and Comparison among structures:
e.g. BOOK_RECORD
book1, book2;
. . . . . . . . . . . . /*
read values for book1 */
book2 = book1.
Thus
if
(book1 = = book2) /* is not allowed */
. . . . . . . .
int
samebook (BOOK_RECORD book1,
BOOK_RECORD book2)
{ int
samebook;
samebook = book1.bk_no == book2.bk_no;
return samebook;
}
Structures as output parameters and the ->
operator.
void update_records( BOOK_RECORD *book,
USER_RECORD *user)
{ (*book).status = BORROWED;
(*user).no_of_books++;
}
book
-> status; is equivalent to (*book).status;
book->pub_date.day;
is equivalent to (*book).pub_date.day;
#include
<stdio.h>
#include
<string.h>
typedef
char STRING[81];
typedef
enum {STUDENT, STAFF, FACULTY} USER_STATUS;
typedef
struct {int id_no;
STRING name;
STRING dept;
int no_of_books;
USER_STATUS status;
} USER_RECORD;
void
get_user_details(USER_RECORD *user)
{ (*user).no_of_books=0;
printf("Enter user
details:\n");
printf("ID_NO: "); scanf("%d",&(*user).id_no);
printf("NAME: "); fflush(stdin); gets((*user).name);
printf("DEPARTMENT: "); gets((*user).dept);
printf("STATUS [0 for student, 1
for staff or 2 for faculty:");
scanf("%d",&(*user).status);
}
main()
{ USER_RECORD user;
get_user_details(&user);
printf("\nThe details you entered
are:\n");
printf("ID_NO: %d\n", user.id_no);
printf("NAME: %s\n", user.name);
printf("DEPARTMENT: %s\n",
user.dept);
printf("STATUS: %d\n", user.status);
printf("NO OF BOOKS %d\n",
user.no_of_books);
return 0;
}
Arrays versus structures:
Unions
e.g. union
int_or_float
{
int i;
float f;
}
union int_or_float a, b, c;
e.g. typedef union
{ int i;
float f;
}NUMBER;
main()
{ NUMBER n;
n.i = 4444;
printf(“i: %d f: %f\n”,n.i, n.f);
n.f=4444.0;
printf(“i: %d f: %f\n”,n.i, n.f);
return 0;
}
i: 4444 f: 0.0000000
i:
-8192 f: 4444.0000