Leveraging Programming Languages: C Lang.

m_ngyeong·2025년 4월 8일
0

정보처리기사 이론

목록 보기
30/36
post-thumbnail

10. Leveraging Programming Languages


C Lang.

✅ 구조체 포인터 체이닝

#include <stdio.h>

struct Node {
    int data;
    struct Node *next;
};

int main() {
    struct Node n3 = {30, NULL};
    struct Node n2 = {20, &n3};
    struct Node n1 = {10, &n2};

    struct Node *head = &n1;
    printf("%d\n", head->next->next->data);
    return 0;
}

🖍️ 30

  • head->next->next->data -> n1 -> n2 -> n3.data

✅ 연결 리스트를 통한 합계 계산

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

struct Node {
    int value;
    struct Node *next;
};

int main() {
    struct Node *a = malloc(sizeof(struct Node));
    struct Node *b = malloc(sizeof(struct Node));
    struct Node *c = malloc(sizeof(struct Node));

    a->value = 5; a->next = b;
    b->value = 10; b->next = c;
    c->value = 15; c->next = NULL;

    struct Node *ptr = a;
    int sum = 0;
    while (ptr != NULL) {
        sum += ptr->value;
        ptr = ptr->next;
    }

    printf("%d\n", sum);
    return 0;
}

🖍️ 30

  • sum = 5 + 10 + 15 = 30

❌ 구조체 포인터 비교

#include <stdio.h>

struct Item {
    int id;
    struct Item *link;
};

int main() {
    struct Item a = {1, NULL};
    struct Item b = {2, &a};
    struct Item c = {3, &b};

    struct Item *ptr = &c;

    if (ptr->link->link == NULL)
        printf("End\n");
    else
        printf("Not End\n");

    return 0;
}

🖍️ Not End

ptr = &c  
ptr->link = &b  
ptr->link->link = &a  
→ &a != NULL"Not End"

❌ 문자열 필터링 (포인터 포함)

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

struct Word {
    char str[20];
    struct Word *next;
};

int main() {
    struct Word w1 = {"cat", NULL};
    struct Word w2 = {"apple", &w1};
    struct Word w3 = {"banana", &w2};

    struct Word *ptr = &w3;

    while (ptr != NULL) {
        if (strchr(ptr->str, 'a') != NULL && strlen(ptr->str) > 4)
            printf("%s\n", ptr->str);
        ptr = ptr->next;
    }

    return 0;
}

🖍️
banana
apple

  • strchr(ptr->str, 'a') != NULL: 문자열에 'a'가 포함
  • strlen(ptr->str) > 4: 문자열의 길이 > 4
  • "cat" : 문자열에 'a'는 포함되지만 문자열의 길이 4가 되지 않음 ∴ false
  • "apple" : 문자열에 'a'가 포함되면서 동시에 문자열의 길이가 4가 넘음 ∴ true
  • "banana": "apple"가 같은 이유 ∴ true

✅ 연결 구조에서 조건 필터 + 합산

#include <stdio.h>

struct Data {
    int num;
    struct Data *next;
};

int main() {
    struct Data d3 = {11, NULL};
    struct Data d2 = {6, &d3};
    struct Data d1 = {4, &d2};

    struct Data *cur = &d1;
    int sum = 0;

    while (cur) {
        if (cur->num % 2 == 0)
            sum += cur->num;
        cur = cur->next;
    }

    printf("%d\n", sum);
    return 0;
}

🖍️ 10

  • 구조체 Dataint num과 다음 노드를 가리키는 포인터 *next를 가지고 있어 → 단일 연결 리스트 구조
  • d1 -> d2 -> d3 순서로 연결되어 있고, 값은 각각 4 → 6 → 11
  • d1 % 2 = 4 % 2 == 0 -> sum += 4
  • d2 % 2 = 6 % 2 == 0 -> sum = 4 + 6
  • d3 % 2 = 11 % 2 != 0

❌ 이중 포인터를 이용한 값 변경

#include <stdio.h>

void update(int **pp) {
    **pp = 99;
}

int main() {
    int a = 5;
    int *p = &a;
    update(&p);
    printf("%d\n", a);
    return 0;
}

🖍️ 99

  • int *p = &a; → 포인터 pa의 주소를 가리킴
  • update(&p);p의 주소를 전달 → 즉, ppint** → "포인터를 가리키는 포인터"
  • *ppp, 즉 &a
  • **pp*(&a) → 결국 a
    **pp = 99a = 99

✅ malloc과 구조체 배열 동적 생성

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

struct Box {
    int size;
};

int main() {
    struct Box *arr = malloc(3 * sizeof(struct Box));
    arr[0].size = 10;
    arr[1].size = 20;
    arr[2].size = 30;

    int total = 0;
    for (int i = 0; i < 3; i++) {
        total += arr[i].size;
    }

    printf("%d\n", total);
    free(arr);
    return 0;
}

🖍️ 60
total = 10 + 20 + 30

✅ 구조체 내 포인터를 이용한 문자열 복사

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

struct Person {
    char *name;
};

int main() {
    struct Person p;
    p.name = malloc(20);

    strcpy(p.name, "Charlie");  // 문자열 복사
    printf("%s\n", p.name);
    free(p.name);
    return 0;
}

🖍️ Charlie

✅ 포인터를 이용한 연결 리스트 길이 구하기

#include <stdio.h>

struct Node {
    int val;
    struct Node *next;
};

int main() {
    struct Node n3 = {3, NULL};
    struct Node n2 = {2, &n3};
    struct Node n1 = {1, &n2};

    struct Node *ptr = &n1;
    int count = 0;

    while (ptr) {
        count++;
        ptr = ptr->next;
    }

    printf("%d\n", count);
    return 0;
}

🖍️ 3

  • 노드 3개

✅ malloc + 연결 리스트 초기화 및 조건 검사

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

struct Animal {
    char name[20];
    struct Animal *next;
};

int main() {
    struct Animal *a = malloc(sizeof(struct Animal));
    struct Animal *b = malloc(sizeof(struct Animal));

    strcpy(a->name, "dog");
    a->next = b;

    strcpy(b->name, "cat");
    b->next = NULL;

    struct Animal *cur = a;
    while (cur != NULL) {
        if (strcmp(cur->name, "cat") == 0)
            printf("Meow\n");
        cur = cur->next;
    }

    free(a);
    free(b);
    return 0;
}

🖍️ Meow

  • if (strcmp(cur->name, "cat") == 0)
    -> if (strcmp("dog", "cat") == 0)
    -> not 0 ∴ cur = cur->next;
  • if (strcmp("cat", "cat") == 0)
    ∴ strcmp == 0
profile
ʚȉɞ

0개의 댓글