Вы находитесь на странице: 1из 19

COMPUTER PROGRAMMING

(CS F111)
LECTURE 34 TUPLE STRUCTURE & UNION

Data Types
Data Types

Derived Types > Arrays > Functions > Pointers

Fundamental Types > Integer > Float > Character

User-defined Types > Structures > unions > Enumerations

Types for Re-use


Types classify (data) values So, each type is a set of values
e.g. int = { m | -2n-1 <= m < 2n-1 } e.g. char = { c | c is an ASCII value }

int and char are basic (primitive) types Arrays are structured (non-primitive) types

Monday, April 21, 2014

Types for Re-use


C Syntax for user-defined types
typedef int Celsius; typedef Celsius WeeklyTemps[7]; typdef int Matrix[100][100]; Matrix M; Type declaration for Matrix type! M[i][j] contains an int

Monday, April 21, 2014

Tuple Types - Motivation


Consider student information Information to store ID (Integer List) Name (String List) AGE (Integer List) CGPA (float list) First attempt: 4 lists (i.e. arrays) Problems: Consider add / delete operations. Operation to be done in 4 arrays separately. 4 arrays are the 4 element values passed as parameters. Data representation does not say the 4 things are related. Better Solution: 1 list of pairs Each pair is of the form (ID, Name, AGE,CGPA)

Monday, April 21, 2014

Student data
ID 2001350 2001355 2001358 2001368 2001370 2001371 2001376 2001378 2001381 RAJANI SHILPA HARENI SOUMYA SOORYA SHILPA PRAVEEN PRADYUMN NAVEEN Name AGE 24 24 25 24 25 24 24 24 25 CGPA 8.6 7.4 8.1 9.1 9.4 8.4 9.3 9.6 8.7

Monday, April 21, 2014

Current Representation (4 different Lists)


ID 2001350 2001355 2001358 2001368 2001370 2001371 2001376 2001378 2001381
Monday, April 21, 2014

Name RAJANI SHILPA HARENI SOUMYA SOORYA SHILPA PRAVEEN PRADYUMN NAVEEN

AGE 24 24 25 24 25 24 24 24 25

CGPA 8.6 7.4 8.1 9.1 9.4 8.4 9.3 9.6 8.7
7

Tuples in C
C Provides structures to represent tuples. struct tag { member 1; member 2; declaration member n; }; The tag is optional can be used subsequently as a shorthand for the part of the declaration in braces struct student { int ID; char Name[100]; int AGE; int CGPA; }; A structure member or tag and an ordinary (non-member) variable can have the same name without conflict. The same member names may occur in different structures struct { } x, y, z; is analogous to int x, y, z;

Monday, April 21, 2014

Structure in C
A struct is a mechanism for grouping together related data items of different types. Recall that an array groups items of a single type. User defined data type or constructed data type. It is a collection of data types that are grouped together under one single name.
April 21, 2014 Dr. Biju K Raveendran@BITS Pilani. 9

Tuples in C
C provides structures to represent tuples. typedef struct { member 1; member 2; no structure tag! member n; } Element; Defines a type named Element which is a structure.
Monday, April 21, 2014 10

Define a structure type without tag-name typedef struct { int ID; char Name[20]; int AGE; float CGPA; } student; student s1; created a struct type student.
4 bytes 20 bytes 4 bytes 8 bytes

Example

s1.ID
Monday, April 21, 2014

s1.Name

s1.AGE

s1.CGPA
11

A structure declaration that is not followed by a list of variables reserve no storage It just declared the template or shape of the structure The tag can be used later for definitions of instances of the structure. The structure member operator . connects the structure name and the member name printf(%d %d, pt2.x, pt2.y); (structure_name.member)

struct point { int x; int y; }; struct point pt; Initializing a structure struct point pt1={100, 200}; OR struct point pt2; pt2.x=100; pt2.y=200;

Monday, April 21, 2014

12

Points to be noted
The closing brace in the structure type declaration must be followed by a semicolon A structure type declaration does not tell the compiler to reserve any space in memory. It only defines the form of the structure i.e. template. Usually structure type declaration appears at the top of the program i.e. before main function before any variables or functions are declared.

Initialization
To store tuple (2003303,Karan,21,8.7) in s1, method is as follows: s1.ID=2003303; // int strcpy(s1.Name,Karan); // string s1.age=21; // int s1.cgpa=8.7; // float Syntax: struct_var.member=val; student s2={2003303, Karan, 21, 8.7};
Monday, April 21, 2014 14

#include<stdio.h> struct point { int x; int y; }; int main() { struct point P1={100,200}; struct point P2; struct point P3; P2.x=20; P2.y=400; printf(Enter the value of P3s\n); scanf(%d %d,&P3.x,&P3.y); printf(The P1s x and y values are \n,P1.x,P1.y); printf(The P2s x and y values are \n,P2.x,P2.y); printf(The P3s x and y values are \n,P3.x,P3.y); return 0; }

Monday, April 21, 2014

15

Assignment and comparison


typedef struct { int length; int width; }Rectangle; Rectangle R1,R2; R1.length=4; R1.width=5; R2 =R1 ? ? Can we compare: R1==R2 ?? Assignment is OK But not equality test
Monday, April 21, 2014 16

Assignment and comparison


#include <stdio.h> typedef struct typedef struct { int length; { int length; int width; int width; }Rectangle1; }Rectangle2; int main(void) { Rectangle1 R1={5,4}; Rectangle2 R2; printf("R1 length = %d, width = %d\n",R1.length, R1.width); R2=R1; printf("R2 length = %d, width = %d\n",R2.length, R2.width); return(0); }
April 21, 2014 Dr. Biju K Raveendran@BITS Pilani. 17

Assignment and comparison


How to compare?? -Field by Field comparison Boolean equals(Rectangle R1, Rectangle R2) { if((R1.length==R2.length) && (R1.width==R2.width) { return TRUE;} return FALSE; }
Monday, April 21, 2014 18

Important Points to note


We cannot initialize individual members inside the structure template The order of values enclosed in braces must match the order of members in the structure definition It is permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. The uninitialized members should be only at the end of the list The uninitialized members will be assigned default values: Zero for integer and floating point numbers and \0 for character and strings
April 21, 2014 Dr. Biju K Raveendran@BITS Pilani. 19

Вам также может понравиться