Structures
INTRODUCTION
• The need for ‘structures’ grew
directly from a desire to group and
manipulate many related but perhaps
diff...
Recording an individual’s
details without a structure
#include<stdio.h>
#include<stdlib.h>
#define BUFSIZE 100
int main(vo...
DEFINING AND USING
STRUCTURES
• The compiler is given details of a
structure template describing the
names and types of da...
struct person {
char name[BUFSIZE];
char address[BUFSIZE];
int age;
float height;
float weight;
};
Key word struct to intr...
struct person p1;
• ‘p1’ is the variable not the
‘person’
Keyword struct to introduce
the structure variable
Structure var...
struct {
char name[BUFSIZE];
char address[BUFSIZE];
int age;
float height;
float weight;
}p1, p2, p3;
Note semicolon after...
struct temp {
int temp;
int x;
} temp;
Structure template
called temp
Member element
called temp
Structure variable
called...
Accessing structure elements:
The dot operator ‘.’
structure_variable . member_element
Dot ‘.’ operator used to
access a m...
Accesing member elements:
the dot operator ‘.’
#include <stdio.h>
struct car {
int capacity;
short doors;
} c1;
int main(v...
Example 1
#include<stdio.h>
struct coord
{ int x;
int y;
}rect1,rect2;
void main (void)
{
rect1.x=100;
rect1.y=200;
printf...
Example 2
#include <stdio.h>
struct data
{ float amount;
char fname[30];
char lname[30];};
struct data rec;
void main (voi...
Example 3
Initializing the structure
#include<stdio.h>
struct sale
{ char customer[20];
char item[20];
float amount;
} mys...
Example 4 Structures as
function arguments
#include<stdio.h>
void f1(struct sample);
struct sample
{ int a,b;
char ch;};
v...
Example 5
# include <stdio.h>
void increase(int *, int);
void main(void){
int i = 500;
increase( &i , i);
printf(“%d”, i )...
Seatwork
Define a data structure to
store the following student
data: gpa, major, address
(consisting of street address,
c...
Upcoming SlideShare
Loading in …5
×

Structures

311 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
311
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Structures

  1. 1. Structures
  2. 2. INTRODUCTION • The need for ‘structures’ grew directly from a desire to group and manipulate many related but perhaps different types of data as a single object or variable. • Structures can have different data types while array is a sequence of elements with the same data type.
  3. 3. Recording an individual’s details without a structure #include<stdio.h> #include<stdlib.h> #define BUFSIZE 100 int main(void) { char name[BUFSIZE]; char address[BUFSIZE]; int age; float height; float weight; … … get_data(name, address, &age, &height, &weight); display_data(name, address, age, height, weight);
  4. 4. DEFINING AND USING STRUCTURES • The compiler is given details of a structure template describing the names and types of data objects or member elements. • The structure template is generally given a name or tag by which it can be referenced later when introducing the structure variable itself.
  5. 5. struct person { char name[BUFSIZE]; char address[BUFSIZE]; int age; float height; float weight; }; Key word struct to introduce structure template Template name is person Member elements of the structure Note use of semicolons ‘;’ The compiler generates no code and no variables at this point in the program.
  6. 6. struct person p1; • ‘p1’ is the variable not the ‘person’ Keyword struct to introduce the structure variable Structure variable is based on template person Note use of semicolons ‘;’ after variable name p1 is the structure variable
  7. 7. struct { char name[BUFSIZE]; char address[BUFSIZE]; int age; float height; float weight; }p1, p2, p3; Note semicolon after variable name ‘;’ Comma ‘,’ separated list of structure variables ‘p1’, ‘p2’ and ‘p3’ Key word struct to introduce structure template Template name is optional If we choose not to name the template, variables must be defined at the same time the template is declared
  8. 8. struct temp { int temp; int x; } temp; Structure template called temp Member element called temp Structure variable called temp Interesting enough, the compiler permits the introduction of a structure template and a structure variable with a common name
  9. 9. Accessing structure elements: The dot operator ‘.’ structure_variable . member_element Dot ‘.’ operator used to access a member element within a structure Once a structure variable has been defined, its member elements – may be individually accessed using the dot ‘.’ (a decimal point operator)
  10. 10. Accesing member elements: the dot operator ‘.’ #include <stdio.h> struct car { int capacity; short doors; } c1; int main(void) { c1.capacity = 2495; c1.doors = 4; printf(“Engine Capacity = %d”, c1.capacity); printf(“Number of doors = %d”, c1.doors); return 0; } Structure Template car Structure Variable c1 Dot operator
  11. 11. Example 1 #include<stdio.h> struct coord { int x; int y; }rect1,rect2; void main (void) { rect1.x=100; rect1.y=200; printf("x=%d, y=%d",rect1.x,rect1.y); } OUTPUT: x=100, y=200
  12. 12. Example 2 #include <stdio.h> struct data { float amount; char fname[30]; char lname[30];}; struct data rec; void main (void) {int i; printf("Enter the donor's first and last names,n"); printf("separated by a space: "); scanf("%s %s", rec.fname, rec.lname); printf("nEnter the donation amount: "); scanf("%f", &rec.amount); printf("nDonor %s %s gave $%.2f.n", rec.fname, rec.lname,rec.amount);} OUTPUT: Enter the donor's first and last names separated by a space: ELMER PINEDA Enter the donation amount: 20000 Donor ELMER PINEDA gave $20000.00
  13. 13. Example 3 Initializing the structure #include<stdio.h> struct sale { char customer[20]; char item[20]; float amount; } mysale = {"Acme Industries", "Left- handed widget", 1000.00}; void main(void) { printf("%sn",mysale.customer); printf("%sn",mysale.item); printf("%.2fn",mysale.amount);} OUTPUT: Acme Industries Left-handed widget 1000.00
  14. 14. Example 4 Structures as function arguments #include<stdio.h> void f1(struct sample); struct sample { int a,b; char ch;}; void main(void) {struct sample arg; arg.a=1000; f1(arg);} void f1(struct sample parm) { printf("%d",parm.a);} OUTPUT: 1000
  15. 15. Example 5 # include <stdio.h> void increase(int *, int); void main(void){ int i = 500; increase( &i , i); printf(“%d”, i ); } void increase( int *i, int j ){ int x; for(x=1; x <=2; x++) *i = *i + j; } Output 1500
  16. 16. Seatwork Define a data structure to store the following student data: gpa, major, address (consisting of street address, city, state, zip), and class schedule (consisting of up to six class records, each of which has description, time, and days components). Define whatever data types are needed.

×