Leveraging Programming Languages: C Lang.

m_ngyeongยท2025๋…„ 4์›” 7์ผ
0
post-thumbnail

10. Leveraging Programming Languages


C Lang.

โœ… ํฌ์ธํ„ฐ ๋ฐฐ์—ด์„ ํ†ตํ•œ ๋ฌธ์ž์—ด ๊ธธ์ด ๊ณ„์‚ฐ

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

int main() {
    char *arr[] = {"apple", "banana", "kiwi"};
    int total = 0;

    for (int i = 0; i < 3; i++) {
        total += strlen(*(arr + i));
    }

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

๐Ÿ–๏ธ 15
strlen(apple) = 5
strlen(banana) = 6
strlen(kiwi) = 4
total = 5 + 6 + 4 = 15

โœ… ๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ€๊ฐ’ ์ฐพ๊ธฐ

#include <stdio.h>

struct Info {
    char name[20];
    int score;
};

int main() {
    struct Info students[3] = {
        {"Kim", 85},
        {"Lee", 92},
        {"Park", 87}
    };

    int max = students[0].score;
    char top[20];

    for (int i = 1; i < 3; i++) {
        if (students[i].score > max) {
            max = students[i].score;
            strcpy(top, students[i].name);
        }
    }

    printf("%s\n", top);
    return 0;
}

๐Ÿ–๏ธ Lee
max = 92 -> int = 1
top = students[1].name = Lee

โœ… ๋ฐฐ์—ด ํฌ์ธํ„ฐ๋กœ 2์ฐจ์› ๋ฐฐ์—ด ์ˆœํšŒ

#include <stdio.h>

int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int (*p)[3] = arr;
    int sum = 0;

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            sum += *(*(p + i) + j);
        }
    }

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

๐Ÿ–๏ธ 21
int = 0
sum = 1 + 2 + 3
int = 1
sum = (1 + 2 + 3) + 4 + 5 + 6

โœ… ๊ตฌ์กฐ์ฒด + ํฌ์ธํ„ฐ ์—ฐ์‚ฐ + ์กฐ๊ฑด

#include <stdio.h>

struct Point {
    int x, y;
};

int main() {
    struct Point arr[] = {{1, 2}, {3, 4}, {5, 6}};
    struct Point *p = arr;
    int result = 0;

    for (int i = 0; i < 3; i++) {
        if ((p + i)->x % 2 == 1) {
            result += (p + i)->y;
        }
    }

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

๐Ÿ–๏ธ 12

  • (p + 0)->x = 1, (p + 1)->x = 3, (p + 2)->x = 5
  • if ((p + i)->x % 2 == 1)๋ฅผ ๋งŒ์กฑํ•˜๋Š” i๋Š” 0, 3, 5
  • result = 2 + 4 + 6 = 12

โœ… ์ฆ๊ฐ์—ฐ์‚ฐ์ž์™€ ํฌ์ธํ„ฐ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉ

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30};
    int *p = arr;
    int a = *p++;
    int b = *p;
    printf("%d %d\n", a, b);
    return 0;
}

๐Ÿ–๏ธ 10 10

  • p++๋Š” ํ›„์œ„ ์—ฐ์‚ฐ์ž ์ด๋ฏ€๋กœ a์—๋Š” ๊ฐ’ 10์ด ์ €์žฅ ๋Œ.
  • b = *p ์ด๋ฏ€๋กœ b๋„ 10์ด ์ €์žฅ ๋Œ.

โœ… ๊ตฌ์กฐ์ฒด ํฌ์ธํ„ฐ ๋ฐฐ์—ด + ์กฐ๊ฑด

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

struct Book {
    char title[20];
    int pages;
};

int main() {
    struct Book b1 = {"C Lang", 350};
    struct Book b2 = {"Data", 200};
    struct Book *books[] = {&b1, &b2};
    int total = 0;

    for (int i = 0; i < 2; i++) {
        if (books[i]->pages > 300)
            total += strlen(books[i]->title);
    }

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

๐Ÿ–๏ธ 6

  • int = 0
    books[0]->pages > 300 = 350 > 300 -> true
    total += strlen(books[0]->title); -> strlen("C Lang")
    โˆด total = 6
    int = 1
    books[1]->pages > 300 = 200 > 300 -> false

โœ… ๋ฐฐ์—ด ํฌ์ธํ„ฐ๋กœ ์กฐ๊ฑด ํƒ์ƒ‰

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

int main() {
    char strs[2][10] = {"cat", "tiger"};
    char (*p)[10] = strs;
    int count = 0;

    for (int i = 0; i < 2; i++) {
        if (strchr(p[i], 't') != NULL) {
            count++;
        }
    }

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

๐Ÿ–๏ธ 2

  • strchr() ํ•จ์ˆ˜๋Š” string์—์„œ ๋ฌธ์ž์˜ ์ฒซ ๋ฒˆ์งธ ํ‘œ์‹œ๋ฅผ ์ฐพ์œผ๋ฉฐ, Null๋กœ ๋๋‚˜๋Š” string์—์„œ ์ž‘๋™.
  • strchr(p[0], 't') : "cat"์—์„œ "t"๊ฐ€ ๋ช‡ ๋ฒˆ์งธ์— ์žˆ๋Š”์ง€๋ฅผ ์ฐพ์Œ -> 2
  • strchr(p[1], 't') : "tiger"์—์„œ "t"๊ฐ€ ๋ช‡ ๋ฒˆ์งธ์— ์žˆ๋Š”์ง€๋ฅผ ์ฐพ์Œ -> 0

โœ… ๊ตฌ์กฐ์ฒด + ํฌ์ธํ„ฐ + ๋ฐ˜๋ณต๋ฌธ + ๋ˆ„์ 

#include <stdio.h>

struct Sensor {
    float temp;
};

int main() {
    struct Sensor sensors[4] = {{23.5}, {25.1}, {22.8}, {24.9}};
    struct Sensor *p = sensors;
    float sum = 0;

    for (int i = 0; i < 4; i++) {
        sum += (p + i)->temp;
    }

    printf("%.1f\n", sum);
    return 0;
}

๐Ÿ–๏ธ 96.3
sum = 23.5 + 25.1 + 22.8 + 24.9 = 96.3

โœ… ๋ฌธ์ž์—ด ํ•จ์ˆ˜ ํ™œ์šฉ ์กฐ๊ฑด๋ฌธ

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

int main() {
    char *list[] = {"apple", "apricot", "banana", "avocado"};
    int count = 0;

    for (int i = 0; i < 4; i++) {
        if (strncmp(list[i], "ap", 2) == 0)
            count++;
    }

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

๐Ÿ–๏ธ 2

  • strncmp(list[i], "ap", 2) โ†’ ๋ฌธ์ž์—ด list[i]์˜ ์•ž 2๊ธ€์ž๊ฐ€ "ap"์™€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋Š” ์กฐ๊ฑด
    โˆด "apple", "apricot"

โœ… ๋‹ค์ฐจ์› ๋ฐฐ์—ด๊ณผ ํฌ์ธํ„ฐ

#include <stdio.h>

int main() {
    int arr[2][2] = {{1, 2}, {3, 4}};
    int *p = &arr[0][0];

    *(p + 2) += 5;
    printf("%d\n", arr[1][0]);
    return 0;
}

๐Ÿ–๏ธ 8

  • *(p + 2) += 5 โ†’ arr[1][0] = 3 + 5 = 8
profile
สšศ‰ษž

0๊ฐœ์˜ ๋Œ“๊ธ€