You are on page 1of 16

Programming Skills-C Programming

STRUCTURES

By
Ms. P.JAYALAKSHMI, AP/CSE
Ms. K.RAMYA, AP/CSE
Programming Skills-C Programming

Structures
Structure Definition
Structure Declaration
Member Access
Memory Layout
Example
Array of structures
Nested structures
Functions and Structures
Structures
• structures are used to group together different types of
variables under the same name. For example you could
create a structure “telephone”:
• struct telephone
• {
• char *name;
• int number;
• };
• To access the members of the structure telephone, you
must use a dot between the structure name and the
variable name
Member Access
• Indirect access s->m: equivalent to (*s).m
– Dereference a pointer to a structure, then return
a member of that structure
– Dot operator has higher precedence than
indirection operator , so parentheses are needed
in (*s).m
(*fido.owner).name or fido.owner->name

• Direct access operator s.m


– subscript and dot operators have same
precedence and associate left-to-right, so we
don’t need parentheses for sam.pets[0].species
Memory layout
struct COST { int amount;
char currency_type[2]; }
struct PART { char id[2];
struct COST cost;
int num_avail; }
layout of struct PART: currency_type

id num_avail

cost

Here, the system uses 4-byte alignment of integers,


so amount and num_avail must be aligned
Four bytes wasted for each structure!
Another example

• Defines a new type


• E.g.,
struct motor {
float volts;
float amps;
int phases;
float rpm;
}; //struct motor
Declaration

• Declaring struct variables


• Declares and sets aside storage for three variables
– p, q, and r – each of type struct motor
struct motor M[25];
• Declares a 25-element array of struct motor;
allocates 25 units of storage, each one big enough
to hold the data of one motor
struct motor *m;
• Declares a pointer to an object of type struct
motor
Arrays of structures

• An ordinary array: One type of data

0 1 2 … 98 99
• An array of structs: Multiple types of data
in each array element.

0 1 2 … 98 99
Example
• We often use arrays of structures.
• Example:
StudentRecord Class[100];
strcpy(Class[98].Name, "Chan
Tai Man");
Class[98].Id = 12345;
strcpy(Class[98].Dept, "COMP");
Class[98].gender = 'M';
Class[0] = Class[98];
access
records
Chan Tai Man
12345 M
COMP

...

0 1 2 … 98 99
Example

• We can use arrays inside structures.


• Example:
struct square{
point vertex[4];
};
square Sq;

• Assign values to Sq using the given


square
x y x y x y x y
Nested structures
• We can nest structures inside structures.
• Examples:
struct point{
double x, y;
};
point P;

struct line{
point p1, p2;
};
line L;

struct triangle{
point p1, p2, p3;
};
triangle T;
Sample Program With Structs

• Illustration
This program illustrates creating structs and then declaring
and using struct variables. Note that struct personal is an
included data type in struct "identity".
*/
#include <stdio.h>
struct personal //Create a struct but don’t reserve space.
{ long id;
float gpa;
};
struct identity //Create a second struct that includes the first one.
{ char name[30];
struct personal person;
};
Main program
int main ( )
{
struct identity js = {"Joe Smith"}, *ptr = &js ;

js.person.id = 123456789 ;
js.person.gpa = 3.4 ;
printf ("%s %ld %f\n", js.name, js.person.id,
js.person.gpa) ;
printf ("%s %ld %f\n", ptr->name, ptr->person.id,
ptr->person.gpa) ;
}
Function with a Structured Input/Output Argument
• For the following function, we have to call it
by
“scan_planet(&current_planet);”
– The input argument is also used to store the result.
uses
• Uses of C structures:
• C Structures can be used to store huge data. Structures
act as a database.
• C Structures can be used to send data to the printer.
• C Structures can interact with keyboard and mouse to
store the data.
• C Structures can be used in drawing and floppy
formatting.
• C Structures can be used to clear output screen
contents.
• C Structures can be used to check computer’s memory
size etc.

You might also like