반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

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

[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;
}
반응형

'[IT] > C++' 카테고리의 다른 글

리눅스 기초  (0) 2020.03.16
5*5행렬에 3*3필터 적용하기  (0) 2020.03.12
배열을 이용한 행렬곱  (0) 2020.03.12
void pointer를 활용한 함수(function)  (0) 2020.03.12
memset / memcpy 활용  (0) 2020.03.11
strtok의 활용  (0) 2020.03.11
3차원 동적할당  (0) 2020.03.11
3차원 배열 활용  (0) 2020.03.11
Comments