#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
#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
= 5if ((p + i)->x % 2 == 1)
๋ฅผ ๋ง์กฑํ๋i
๋ 0, 3, 5result
= 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"๊ฐ ๋ช ๋ฒ์งธ์ ์๋์ง๋ฅผ ์ฐพ์ -> 2strchr(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