1
خدا بنام
نویسی برنامه )C (21814زبان
Lecture 14
Structures
2
نویسی برنامه )C (21814زبان
Structures
ترکیبکردن • روشهای ترین پرکاربرد از یکیزبان در ها از Cداده می Structureاستفاده ها
باشد.
یک • از استفاده روشبرخالفحالت این درها داده توان می ) )آرایه، انواع با ها متغیر
. نمود استفاده جا یک در مختلفرا
یک • بخشهای از یک هر جا این Structureدر. است جداگانه نام دارای ها آرایه برخالف
3
نویسی برنامه C (21814 ( - Structureزبان
آن • از روشاستفاده این مهم های کاربرد از. باشد اطالعاتیمی بانکهای ایجاد برای
جلسه • این درنمایش – Structureنحوه
آنها – روی به اجرا قابل های عملیاتآنها – با ها آرایه ترکیب نحوه
می قرار بررسی گیرند. مورد
4
نویسی برنامه C (21814 ( - Structureزبان
از • دهد Structureاستفاده می را امکان این ها / / مورد فرد مختلفیکشی خاصیتهای که. شوند ارایه هم با مرتبط صورت به مطالعه
• : بانک در ورزشکار مختلفیک اطالعات مثال. ورزشی های فدراسیون اطالعاتی
متغیر • متعددی Structureیک تعداد شاملتعریفشده اعضای )slots)فضای که است
Structure میشوند .)members or fields)نامیده
5
نویسی برنامه C (21814 ( - Structureزبان
اعضای • از یک از Structureهر یکی بیانگر . باشند می نظر مورد خصوصیاتشی
بانکهای • در رفته کار به های زبان دریک یک Structureاطالعاتی، نام نامیده Recordبه
. میشود
6
نویسی برنامه C (21814 ( - Structureزبان
Structure Declaration
struct tag {
type variable;
type variable;
…
} variable, variable, …;
optional (but need at least one).
called members
7
نویسی برنامه C (21814 ( - Structureزبان
شامل Structure typeیک •
–Structure type specification کلمه . structباکه – اختیاری نام می tag nameیک نامیده
شود.داخل } {.– در اعضا تعریف–.) اختیاری ) ها متغیر نام
8
نویسی برنامه C (21814 ( - Structureزبان
روشتعریف • همانند دقیقا اعضا تعریف نحوه. است متغیر یک
یک • عضو دارای structureدو توانند typeمی. باشند مشابه
یک • اعضای از یک توانند structureهر می. شوند زده صدا مجزا بصورت
به • منحصر نام دارای بایستی اعضا از یک هر. باشند فردی
9
نویسی برنامه C (21814 ( - Structureزبان
:مثالstruct course }
char * dept; /* e.g. “IE" */char * title; /* e.g. "Intro to C Programming"*/int number; /* e.g. 21814 */
int section; /* e.g. 1 */ {;
• This defines a type only )struct course(, no memory used or variables defined. This is not a declaration!!
10
نویسی برنامه C (21814 ( - Structureزبان
Variables of type 'course‘
struct course }char * dept; /* e.g. IE */
char * title; /* e.g. Intro to C Programming*/
int number; /* e.g. 21814 */
int section; /* e.g. 1 */
{ c1, c2, c3;
• Defines and Declares three 'course-type' variables.
11
نویسی برنامه C (21814 ( - Structureزبان
اعضای به دهی مقدار Structure:نحوهc1.dept = "IE"; /* called 'dot' notation */
c1.title = "Intro to C Programming";
c1.number = 21814;
c1.section = 1;
printf)“%s %s %d %d\n”, c1.dept, c1.title, c1.number, c1.section(;
IE Intro to C Programming 21814 1
12
نویسی برنامه C (21814 ( - Structureزبان
نوع میان آنها Structureتفاوت تعریف نحوه و
struct course } /* this defines the new 'struct course' type */
char * dept; /* e.g. IE */
char * title; /* e.g. Intro to C Programming*/
int number; /* e.g. 21814 */
int section; /* e.g. 1 */
{;
struct course courses[100]; /* an array )collection( of 100 courses */
courses[0].dept = “English”; /* note syntax */
courses[1].dept = “Psychology”; /* etc. */
13
نویسی برنامه C (21814 ( - Structureزبان
c:10
struct card { char *suit; int val;} c;
c.suit = "clubs";c.val = 10;
&c.suit
&c.val
clubs\0
123456:
123456
14
نویسی برنامه C (21814 ( - Structureزبان
struct card }
char *suit;
int val;
{ c;
: باشند می هم روشمانند دو اینstruct card }
char *suit;
int val;
{;
struct card c;
15
نویسی برنامه C (21814 ( - Structureزبان
تعریفیک • در Structureهنگام توان مینام زیر مانند هایی تعریف Structureحالت را
نکرد.struct }
char * name;
char *tel_no;
{ tel_list[100]; /* list of 100 names & telnos */
این • به متغیر نوع یک فقط حالتی چنین در. است تعریفشده صورت
16
نویسی برنامه C (21814 ( - Structureزبان
تعریفیک • حداقل Structureهنگام نوشتن ،. است ضروری دو این از یکی
struct }
char * name;
char *tel_no;
{;
• This is useless -- no variables of this type and no tag to use to declare variables.
17
نویسی برنامه C (21814 ( - Structureزبان
typedef declaration
گذاری • نام برای مناسب روشهای از typeیکیاز استفاده موجود .typedefهای باشد می
typedef old_type new_type_name;
• Can be used to rename all current types.• char, int, float, double, struct, and pointers to
these.• Mostly used with struct type
18
نویسی برنامه C (21814 ( - Structureزبان
typedef struct }
char * dept;
char * title;
int number;
int section;
{ course_t;
/* course_t is new type and can be used in declarations */
int main)void( }
course_t IE21814, cs161, cs162; /* declares 3 courses */
IE21814.title = "Introduction to C Programming";
etc.
19
نویسی برنامه C (21814 ( - Structureزبان
اعضای به دهی مقدار Structure:نحوه
:مثالtypedef struct Lumber}
char type[11]; //type of wood
int height, width, length;
float price;
int quantity;
{ lumber_t;
lumber_t sale;
lumber_t plank = }“white oak”, 1, 6, 8, 5.82, 158{;
20
نویسی برنامه C (21814 ( - StructureزبانTypedef or Tag?
typedef struct {char * dept;char * title;int number;int section;
} course_t;
/* 'course_t' is type*/course_t c1, c2, c3;
struct course {char * dept;char * title;int number;int section;
};
/* 'struct course' is type*/struct course c1, c2, c3;
21
نویسی برنامه C (21814 ( - Structureزبان
توجه:
نویسی برنامه زبان برای Cدر دستوریدو . Structure مقایسه صورت در ندارد وجود
دو مقایسه به برنامه structureنیاز بایستی. داد انجام نویسی
22
نویسی برنامه C (21814 ( - Structureزبان
typedef struct }
char * dept; char * title;
int number; int section;
{ course_t;
int main)void ( }
course_t c, *p;
p = &c; /* dept member in struct pointed to by p */
p->dept = "cs";
p->number = 151;
printf)"%s %d", c.dept, c.number(;
return 0;
{
prints:cs 151
23
نویسی برنامه C (21814 ( - Structureزبان
:مثالtypedef struct Lumber}
char type[11]; int height, width, length;
float price; int quantity;
{ lumber_t;
lumber_t plank = }“white oak”, 1, 6, 8, 5.82, 158{;
float cost;
Lumber_t *p;
P = &plank;
Cost = p->price;
24
نویسی برنامه C (21814 ( - Structureزبان
یک اعضای به دستیابی :structureروشهایمستقیم – دسترسی
printf)“ %i \n “,plank.height(;
از – از : pointerاستفاده روش این arrowدر
operator اعضای به دسترسی structureبرای. شود می استفاده
printf) “ %i \n”, p-> height(;
25
نویسی برنامه C (21814 ( - Structureزبان
یک مقادیر انتقال :structureروش دیگری به
typedef struct Lumber}
char type[11]; int height, width, length;
float price; int quantity;
{ lumber_t;
lumber_t sale;
lumber_t plank = }“white oak”, 1, 6, 8, 5.82, 158{;
sale = plank;
26
نویسی برنامه C (21814 ( - Structureزبان
انتقال به structureنحوه :functionها ها توان • و structureمی به pointerها که هایی
عنوان structureیک به را کنند می اشارهargument یک . functionهای برد کار به
یک • که آنجا متعددی structureاز اجزای دارایکه است بهتر باشد، تواند آن pointerمی
. شود استفاده
27
نویسی برنامه C (21814 ( - Structureزبان
typedef struct } /* note: comes before print_card prototype */char * suit;int val;
{ card_t; /* function prototype uses 'card' type */
void print_card)card_t c(;
int main)void( }card_t c = }"clubs", 10{; /* note: initialization! */print_card)c(; /* call by value */return 0;
{
void print_card)card_t c( } /* this c is a copy of the c in main */printf)"%d of %s\n", c.val, c.suit(;
{
28
نویسی برنامه C (21814 ( - Structureزبان
typedef struct } char * suit; int val; { card_t;
void print_card)card_t c(;void init_card)card_t *p, char * suit, int val(;
int main)void( }card_t c;init_card)&c, "clubs", 10(; /* call by reference */print_card)c(; /* call by value */return 0;
{void print_card)card_t c( }
printf)"%d of %s\n", c.val, c.suit(;{void init_card)card_t *p, char * suit, int val(} /* p points to c in
main */p->suit = suit;p->val = val;
{
29
نویسی برنامه C (21814 ( - Structureزبان
:تمرینبه دانشجویان، لیست که بنویسید ای برنامه
ورودی فایل یک از را مربوطه اطالعات همراهمیان های واریانسامتحان و میانگین گرفته،
. کاربر سپساز کند محاسبه را ترم پایان و ترمنام ) نام، برحسب را لیست کردن مرتب نحوه
ترم، میان نمره دانشجویی، شماره خانوادگی، . ) برنامه این در نماید مرتب ترم پایان نمره یا و
یک در را دانشجویان ذخیره structureاطالعاتکنید.
30
نویسی برنامه )C (21814زبان
:تمرینبصورت تابع یک زدن روشصدا ترین مناسب
prototype void myFun( student_t x);
است ؟کدام )myFun )Ali_Jamshidi الف-
.bmyFun )&Ali_Jamshidi(
.emyFun )student_t Ali_Jamshidi(
)myFun )*Ali_Jamshidi د-
Recommended