Professional Documents
Culture Documents
Activity No 13 Structures
Activity No 13 Structures
13 - Structures
Course Code: CPE007 Program:
Course Title: Programming Logic and Design Date Performed:
Name: Date Submitted:
Section: Instructor:
1. Objective:
Structure Definitions
Structures are derived data types - they are constructed using objects of other types. Consider the following
structure definition:
struct card {
char *face;
char *suit;
};
Keyword struct introduces the structure definition. The identifier card is the structure tag. The structure tag
names the structure definition and is used with the keyword struct to declare variables of the structure
type. In this example, the structure type is struct card. Variables declared within braces of the structure
definition are the structure’s members. Members of the same structure must have unique names, but two
different structures may contain members of the same name without conflict. Each structure definition must
end with a semicolon.
struct employee{
char firstname[20];
char lastname[20];
int age;
char gender;
double hourlySalary;
struct employee2 person; //ERROR
struct employee2 *ePtr; //pointer
};
Initializing Structures
Structures can be using initializer lists as with arrays. To initialize a structure, follow the variable name in
the structure declaration with an equals sign and a brace-enclosed, comma-separated list of initializers. For
example, the declaration
creates a variable a to be of type struct card (as defined previously) and initializes member face to
“Three” and member suit to “Hearts”. If there are fewer initializers in the list than members in the
structure, the remaining members are automatically initialized to 0 (or NULL if the member is a pointer).
printf(“%s”, a.suit);
#include <stdio.h>
struct card {
char *face;
char *suit;
};
int main()
{
struct card a;
struct card *aPtr;
a.face = "Ace";
a.suit = "Spades";
aPtr = &a;
Sample Output:
To pass a structure call by reference, pass the address of the structure variable . Arrays of structures - like
other arrays - are automatically passed call by reference.
To pass an array call by value, create a structure with the array as a member. Since structures are passed
call by value, the array is passed call by value.
TYPEDEF
The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data
types. Names for structure types are often defined with typedef to create shorter type names. For example
the statement
defines the new type name Card as a synonym or type struct card. C programmers often use typedef to
define a structure type so a structure tag is not required. For example, the following definition
typedef struct {
char *face;
char *suit;
} Card;
creates the structure type Card without the need for a separate typedef statement.
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
return 0;
}
Output:
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books book );
int main( ) {
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
return 0;
}
Output:
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
int main( ) {
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
return 0;
}
Output:
4. Resources:
Personal Computer
C Language Compiler
5. Activities:
1. Write a program that reverses the order of the bits in an
unsigned integer value. The program should input the value
from the user and call function reverseBits to print the
bits in reverse order. Print the value in bits both before
and after the bits are reversed to confirm that the bits
are reversed properly.
2. Write a program that creates a function multiple that
determines if the integer entered from a keyboard is a
multiple of some integer x.