c 언어 포인터와 구조체 질문 입니다.

익명 사용자의 이미지

이런 글을 올려서 정말 죄송합니다.
근데 정말 정말 모르겠어서 글 올립니다.
대체 int person_list_length(person_t *list) 이 부분에
뭘 채워넣으라는 의미 인가요?
현재 상태에서 출력되 잘 되는데 말입니다.
이런 두서 없는 글 올려서 죄송합니다.
물어볼 곳이 없어서 올려봅니다.

#define TRUE 1
#define FALSE 0
 
typedef struct person_s {
    char    name[31];
    int     age;
    float   height;
 
    struct person_s *next;      // Next person on the list
} person_t;
 
 
void free_person_list(person_t *);
int person_list_length(person_t *);
 
person_t *read_person(int);
person_t *read_person_list();
 
/*
  Pretend to read a person from a file.  For the purposes of this
  program, we are passed in an int telling us which person is read.  We
  make up a fake person based on num, rather than actually reading
  from a file.
 */
 
person_t *read_person(int num) {
    if (num < 0 || num >= 3) {
        return NULL;        // Pretend it's end of file
    }
    person_t *result = (person_t *) malloc(sizeof(person_t));
    switch(num) {
        case 0:
            strcpy(result->name, "John");
            break;
        case 1:
            strcpy(result->name, "Chihiro");
            break;
        case 2:
            strcpy(result->name, "Merced");
            break;
        default:
            printf("PROGRAM ERROR, ABORTING!");
            exit(1);
    }
    result->age = 18 + num;
    result->height = 5 + 0.1f * num;
    return result;
}
 
/*
 * Pretend to read a list of person instances from a file
 */
person_t *read_person_list() {
    person_t *list;
    person_t **to_be_read = &list;
    int i = 0;
    while (TRUE) {
        person_t *to_add = read_person(i++);
        *to_be_read = to_add;
        if (to_add == NULL) {
            return list;
        }
        to_be_read = &((*to_be_read)->next);
    }
}
 
/*
 * Free a list of person entries
 */
void free_person_list(person_t *list) {
    while (list) {
        person_t *next = list->next;
        free(list);
        list = next;
    }
}
 
/*
  Return the number of person_t objects on the list
  that is passed in.
 */
int person_list_length(person_t *list) {
 
 
  /////여기에 정답을 넣어야만 합니다./////
 
 
}
 
 
 
int main() {
    person_t *person_list;
    person_list = read_person_list();
    int len = person_list_length(person_list);
    printf("person_list has %d person instances.\n", len);
    free_person_list(person_list);
}

raymundo의 이미지

코멘트에 있는 그대로, 인자로 받은 list에 있는 person_t 객체의 개수를 반환하는 코드를 작성하시면 되겠네요.

출력에 "person_list has ** person instances."에서 ** 자리에 정확한 숫자가 나오도록 말이죠. 지금 코드라면 3이 나와야겠네요.

좋은 하루 되세요!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.