struct dalah kumpulan dari berbagai tipe data menjadi satu.
cara pendeklarasian :
struct nama{
int a;
float b ; // berbeda bisa dijadikan dalam 1 struct;
char z;
};
contoh pemakaian sebagai berikut :
struct nama{
int a;
float b ;
char z;
};
void main() {
nama.a =5;
nama.b =4.0;
nama.z = 'a';
}
contoh lain berupa Array of struct
#include
#include
struct
{
char what[25];
int legs, arms;
} object[6];
int main()
{
int index;
strcpy(object[0].what, "human being");
object[0].legs = 2;
object[0].arms = 2;
strcpy(object[1].what, "dog");
object[1].legs = 4;
object[1].arms = 0;
strcpy(object[2].what, "television set");
object[2].legs = 4;
object[2].arms = 0;
strcpy(object[3].what, "chair");
object[3].legs = 4;
object[3].arms = 2;
strcpy(object[4].what, "centipede");
object[4].legs = 100;
object[4].arms = 0;
strcpy(object[5].what, "spider");
object[5].legs = 6;
object[5].arms = 0;
for(index = 0 ; index < 6 ; index++)
{
printf("A %s has %d legs and %d arms.\n", object[index].what,
object[index].legs, object[index].arms);
}
return 0;
}
contoh kombinasinya , menggunakan pointer:
#include
#include
struct
{
char what[25];
int legs, arms;
} object[6], *point;
int main()
{
int index;
strcpy(object[0].what, "human being");
object[0].legs = 2;
object[0].arms = 2;
strcpy(object[1].what, "dog");
object[1].legs = 4;
object[1].arms = 0;
strcpy(object[2].what, "television set");
object[2].legs = 4;
object[2].arms = 0;
strcpy(object[3].what, "chair");
object[3].legs = 4;
object[3].arms = 2;
strcpy(object[4].what, "centipede");
object[4].legs = 100;
object[4].arms = 0;
strcpy(object[5].what, "spider");
object[5].legs = 6;
object[5].arms = 0;
point = object;
for(index = 0 ; index < 6 ; index++)
{
printf("A %s has %d legs and %d arms.\n", point->what,
point->legs, point->arms);
point++;
}
return 0;
}
#include
#include
struct child
{
char initial;
int age;
int grade;
} *boy, *girl;
int main()
{
boy = (struct child *)malloc(sizeof(struct child));
boy->initial = 'R';
boy->age = 15;
boy->grade = 75;
girl = (struct child *)malloc(sizeof(struct child));
girl->age = boy->age - 1;
girl->grade = 82;
girl->initial = 'H';
printf("%c is %d years old and got a grade of %d\n",
girl->initial, girl->age, girl->grade);
printf("%c is %d years old and got a grade of %d\n",
boy->initial, boy->age, boy->grade);
return 0;
}
#include
#include
struct child
{
char initial;
int age;
int grade;
} *kids[12];
int main()
{
int index;
for (index = 0 ; index < 12 ; index++)
{
kids[index] = (struct child *)malloc(sizeof(struct child));
kids[index]->initial = 'A' + index;
kids[index]->age = 16;
kids[index]->grade = 84;
}
kids[3]->age = kids[5]->age = 17;
kids[2]->grade = kids[6]->grade = 92;
kids[4]->grade = 57;
*kids[10] = *kids[4]; /* Structure assignment */
for (index = 0 ; index < 12 ; index++)
printf("%c is %d years old and got a grade of %d\n",
kids[index]->initial, kids[index]->age,
kids[index]->grade);
return 0;
}
Kamis, Mei 15, 2008
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar