34 พิมเขียวข้อมูล อาเรย์ typedef


  • Syntax: typedef struct <name> { /*definition*/ } Synonyms
  • Keyword : typedef , struct
  • การสร้างอาเรย์ของ struct เหมือนการสร้างอาเรย์ทั่วไป

#include <stdio.h>
#include <string.h>
typedef struct student
{
char name[20] ;
int id;
} Student;
int main(void)
{
char student[][20] = { "David",
"Ben" ,
"Mark" ,
"Tom" ,
"Elizabeth" ,
"Peter"
};
int i;
// typedef int Age;
Student std[6];
//struct student std[6];
for(i= 0 ; i < 6 ; i++)
{
strcpy(std[i].name,student[i]);
//std[i].name = student[i];
std[i].id = i + 100;
}
for(i = 0 ; i < 6 ; i++)
{
printf("%s , %d\n",std[i].name, std[i].id);
}
return 0;
}