✅ 목표 (Goal)

✅ 내용 (Contents)

🔹 인자로 받는 참조가 NULL이면 뭘 해서는 안돼!

근데 위와 같은 코드보다,

아래와 같이 인자로 받는 참조가 NULL일때 그냥 함수를 종료하는 용도로 자주 사용된다.

student가 NULL이면 뭘 할필요가 없다. (= 함수 내부로 들어갈 필요가 없다.)

이런 코드를 보통 가드.. 라고 한다.

#include <stdio.h>

typedef struct _student_t {
    int no;
} student_t;

void printStudent(student_t* student) {
    if (student != NULL) {
        printf("%d", student->no);
    }
}

int main() {
    student_t student;
    student.no = 11;
    printStudent(&student);
    return 0;
}

🔹 위와 동일하지만, 보기가 더 좋지.

printStudent 함수를 위와 같이 적어도 맞지만, 아래와 같이 적어주는게 더 보기 좋다.

아래 예제에서 한번 더 보고 마치도록 하자.

#include <stdio.h>

typedef struct _student_t {
    int no;
} student_t;

void printStudent(student_t* student) {
    if (student == NULL) { return; }
    printf("%d", student->no);
}

int main() {
    student_t student;
    student.no = 11;
    printStudent(&student);
    return 0;
}