Академический Документы
Профессиональный Документы
Культура Документы
(CS F111)
LECTURE 34 TUPLE STRUCTURE & UNION
Data Types
Data Types
int and char are basic (primitive) types Arrays are structured (non-primitive) types
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
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;
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;
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; }
15