You are on page 1of 8

Activity No.

13 - Structures
Course Code: CPE007 Program:
Course Title: Programming Logic and Design Date Performed:
Name: Date Submitted:
Section: Instructor:
1. Objective:

This activity aims to develop programming skills using structures

2. Intended Learning Outcomes (ILOs):


The students should be able to:
2.1 Create and use structures
2.2 Pass structures to functions call by value and call by reference
3. Discussions
Structures
Structures are collections of related variables- sometimes referred to as aggregates - under one name.
Structures may contain variables of many different data types - in contrast to arrays that contain only
elements of the same data type. Structures are commonly used to define records to be stored in files.

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.

Structure members can have a variety of data types:

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

struct card a = {“Three”, “Hearts”};

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).

Accessing Members of Structures


Two operators are used to access members of structures: The structure member operator (.)- also called
the dot operator - and the structure pointer operator (- >) - also called the arrow operator. The structure
member operator accesses a structure member via the structure variable name. For example, to print
member suit of structure a from the preceding declaration, use the statement

printf(“%s”, a.suit);

this is the same as:

printf(“%s”, aPtr-> suit);

Sample Code of Using the structure member and structure pointer


operators:

#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;

printf("%s%s%s\n%s%s%s\n%s%s%s\n", a.face, " of ",


a.suit,
aPtr->face, " of ", aPtr->suit, (*aPtr).face, " of ",
(*aPtr).suit);
return 0;
}

Sample Output:

Using Structures with Functions


Structures may be passed to functions by passing individual structure members, by passing an entire
structure or by passing a pointer to a structure. When structure s or individual structure members are
passed to a function, they are passed call by value. Therefore, the members of a caller’s structure cannot
be modified by the called function.

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

typedef struct card Card;

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.

Sample code of Accessing Structure Members:

#include <stdio.h>
#include <string.h>

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};

int main( ) {

struct Books Book1; /* Declare Book1 of type Book


*/
struct Books Book2; /* Declare Book2 of type Book
*/

/* 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;

/* print Book1 info */


printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */


printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);

return 0;
}

Output:

Sample Code of Structure Function Arguments:

#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( ) {

struct Books Book1; /* Declare Book1 of type Book


*/
struct Books Book2; /* Declare Book2 of type Book
*/

/* 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;

/* print Book1 info */


printBook( Book1 );

/* Print Book2 info */


printBook( Book2 );

return 0;
}

void printBook( struct Books book ) {

printf( "Book title : %s\n", book.title);


printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
}

Output:

Sample Code of Pointers to Structures:

#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( ) {

struct Books Book1; /* Declare Book1 of type Book


*/
struct Books Book2; /* Declare Book2 of type Book
*/

/* 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;

/* print Book1 info by passing address of Book1 */


printBook( &Book1 );

/* print Book2 info by passing address of Book2 */


printBook( &Book2 );

return 0;
}

void printBook( struct Books *book ) {

printf( "Book title : %s\n", book->title);


printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}

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.

6. Assessment (Rubric for Laboratory Performance):

You might also like