View Full Version : Re: เรื่อง โครงสร้าง (struct) for C Language
asylu3
27-03-2004, 02:13 AM
เป็นคำถามที่น้องเขาถามมาทาง email นะครับเลยเอามาตอบให้อ่านกันดู
struct atruc para{
type name1;
type name2;
.
.
.
type namre-n
}struc variables;
ตัวอย่าง
struct student{
char name[30];
char lastname[60];
int age;
}stu1;
นั้นหมายถึงอะไรครับพี่ ผมยังไม่เคยเห็นเลยพี่ ช่วยตอบด้วยครับ
asylu3
27-03-2004, 02:21 AM
ก็เป็นการสร้าง "โครงสร้างตัวแปร" แบบพิเศษชนิดนึงขึ้นมานะครับ
จุดประสงค์เพื่ออำนวยความสะดวกในการ สร้างตัวแปรบางประเภท ยกตัวอย่างเช่น
struct Record {
char name[20];
int age;
}
จากตัวอย่างด่านบนเป็นการสร้างตัวแปรชื่อ Record ขึ้นมา เพื่อความสะดวกในการเขียนโปรแกรมในการจัดเก็บข้อมูล
ตัวอย่างวิธีการนำไปใช้มีดังนี้ครับ
Click Here (http://seri.kmutt.ac.th/cs02/citec/forum2/misc.php?action=downloadfile&FileID=59)
ความจรืงแล้ว struct ก็คล้ายๆกับ class นั้นแหล่ะครับเพียงแต่ไม่มีความสามารถด้าน OO
Kittipong_kang
27-03-2004, 09:11 PM
พี่แล้วตัว }stu1; ของเจ้านี้คืออะไรพี่ มันใช้ยังไงอะ
struct atruc para{
type name1;
type name2;
.
.
.
type namre-n
}struc variables;
ตัวอย่าง
struct student{
char name[30];
char lastname[60];
int age;
}stu1;
Kittipong_kang
27-03-2004, 09:11 PM
พี่แล้วตัว }stu1; ของเจ้านี้คืออะไรพี่ มันใช้ยังไงอะ
struct atruc para{
type name1;
type name2;
.
.
.
type namre-n
}struc variables;
ตัวอย่าง
struct student{
char name[30];
char lastname[60];
int age;
}stu1;
Kittipong_kang
27-03-2004, 09:14 PM
พี่แล้วตัว }stu1; ของเจ้านี้คืออะไรพี่ มันใช้ยังไงอะ
struct atruc para{
type name1;
type name2;
.
.
.
type namre-n
}struc variables;
ตัวอย่าง
struct student{
char name[30];
char lastname[60];
int age;
}stu1;
แล้วอย่าลือลบ ถามต่อ 2 ตัวที่เป็น New Tropic ออกด้วยนะครับ
Kittipong_kang
27-03-2004, 09:22 PM
พี่แล้วแบบนี้หละ
struct student{
char name[30];
char lastname[30];
int age;
}stu1;
struct student *p;
และ
struct student{
char name[30];
char lastname[30];
int age;
}stu1,*p=&stu1;
และ
struct student{
char name[30];
char lastname[30];
int age;
}stu1;
stu1 *stu2;
Kittipong_kang
27-03-2004, 09:40 PM
พี่การที่ใช้ การประการตัวแปรรับข้อมูล มันจะรับได้เฉพาะ Pointer เท่านั้นเหรอครับ
asylu3
28-03-2004, 01:39 AM
struct student{
char name[30];
char lastname[30];
int age;
}stu1;
ไอ้ stu1 ก็คือการประกาศตัวแปร struct ขึ้นมาใช้เลยทันทีนั้นแหล่ะครับ
แล้วการที่ใช้ pointer เพราะว่าต้องการให้รับค่าเป็นตัวอักษรนะครับ ไม่จำเป็นต้องใช้เสมอ
Powered by vBulletin® Version 4.2.5 Copyright © 2026 vBulletin Solutions Inc. All rights reserved.