C 언어 함수에 구조체 변수 전달과 반환

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct s_student
{
char name[20];
int age;
long number;
};
void print_student_info(struct s_student student);
void input_student_info(struct s_student *pStudent);

int main(int argc, char const *argv[])
{
/* code */
struct s_student student = {"NAM", 28, 60121315};

printf("Initial Value\n");
print_student_info(student);

input_student_info(&student);
printf("After Calling Function input_student_inf() \n");
print_student_info(student);

return 0;
}

void print_student_info(struct s_student student)
{
printf(
"Name : %s \t Age : %d \t Number : %ld\n",
student.name, student.age, student.number
);
}

void input_student_info(struct s_student * pStudent)
{
if(NULL != pStudent)
{
printf("Student Name ? ");
scanf("%s", pStudent->name);
printf("Student Age ? ");
scanf("%d", &pStudent->age);
printf("Student Number ? ");
scanf("ld", &pStudent->number);
printf("Call by Value\n");
print_student_info(*pStudent);
}
else
{
printf("NULL !!!\n");
}
}

> Executing task: ./input_student_info.exe <

Initial Value
Name : NAM       Age : 28        Number : 60121315
Student Name ? Dong-Gil
Student Age ? 28
Student Number ? Call by Value
Name : Dong-Gil          Age : 28        Number : 60121315
After Calling Function input_student_inf()
Name : Dong-Gil          Age : 28        Number : 60121315



#include <stdio.h>
#include <stdlib.h>
struct s_student
{
char name[20];
int age;
long number;
int score;
};

void print_student_info(struct s_student student);
struct s_student get_max_score_student(struct s_student *pStudent,
int student_count);

int main(int argc, char const *argv[])
{
struct s_student students[] = {
{"NAM", 28, 60121315, 100},
{"HOON", 29, 60121316, 100},
{"MOON", 50, 60121317, 60}
};

struct s_student student = {0};
int count = 0;

count = sizeof(students)/ sizeof(struct s_student);
student = get_max_score_student(students, count);

printf("Highest Scored Student\n");
print_student_info(student);
/* code */
return 0;
}

void print_student_info(struct s_student student)
{
printf("Name : %s \t Age : %d \t Num : %ld \t Score : %d",
student.name, student.age, student.number, student.score);
}

struct s_student get_max_score_student(struct s_student *pStudent,
int student_count)
{
struct s_student student = {0};
int i = 0;
int max_score = pStudent->score;
if(NULL != pStudent){
for(i = 0 ; i < student_count; i++){
if(max_score <= (pStudent + i)->score)
{
student = *(pStudent + i);
}
}
}
else
{
printf("NULL 오류 !!");
}

return student;
}


> Executing task: ./get_max_score_student.exe <

Highest Scored Student
Name : HOON      Age : 29        Num : 60121316          Score : 100







댓글

이 블로그의 인기 게시물

Spring Boot Actuator readiness, liveness probes on k8s

About Kafka Basic

sneak peek jitpack

About idempotent

About G1 GC

About ZGC

About JVM Warm up

I need to know a little JVM

HackerRank Java Between Two Sets

Java - HashMap (feat. LinkedList, Tree.. maybe Later)