[IT]/C++

구조체 안에서 함수 사용하기

givemebro 2020. 3. 11. 15:33
반응형
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student {
	char name[64];
	int id;
	double score;
	void(*display)(struct student);
}student;
void myprint(student s) {
	printf("%s> %d , %.2f\n", s.name, s.id, s.score);
}
int main() {
	int num;
	scanf("%d", &num);
	student *boys = (student*)malloc(sizeof(student)*num);

	// student boys[3];

	char lines[3][100] = { "did,1,53.3","jad,4,23.4","gh,2,63.3" };
	char *p;
	for (int idx = 0; idx < num; idx++) {
		p = strtok(lines[idx], ",");
		strcpy(boys[idx].name, p);
		p = strtok(NULL, ",");
		boys[idx].id = atoi(p);
		p = strtok(NULL, ",");
		boys[idx].score = atof(p);

		printf("%5s / %5d / %5f\n", boys[idx].name, boys[idx].id, boys[idx].score);
	}


	return 0;
}
반응형