#include <stdio.h>
struct Point{
int x;
int y;
};
void PrintPoint(Point* p) {
printf("(%d, %d)\n", p->x, p->y); //์ฃผ์๋ฅผ ์ฌ์ฉํ ๋ p->x ์ ์๋ฅผ ์ฌ์ฉํ ๋ pt1.x
}
int main() {
Point pt1 = { 1, 2 };
PrintPoint(&pt1);
printf("(%d, %d)\n", pt1.x, pt1.y);
}
#include <stdio.h>
struct Point {
int x;
int y;
};
void PrintPoint(Point* p) {
printf("(%d, %d)\n", p->x, p->y);
}
void SetPoint(Point* p, int x, int y) {
p->x = x;
p->y = y;
}
int main() {
Point pt1 = { 1, 2 };
PrintPoint(&pt1);
SetPoint(&pt1, 4, 5);
PrintPoint(&pt1);
PrintPoint(&pt1);
}
#include <stdio.h>
struct Point {
int x;
int y;
};
void PrintPoint(Point* p) {
printf("(%d, %d)\n", p->x, p->y);
}
void SetPoint(Point* p, int x, int y) {
p->x = x;
p->y = y;
}
void ResetPoint(Point* p) {
p->x = 0;
p->y = 0;
}
int main() {
Point pt1 = { 1, 2 };
PrintPoint(&pt1);
SetPoint(&pt1, 4, 5);
PrintPoint(&pt1);
ResetPoint(&pt1); // 0,0
PrintPoint(&pt1);
}
#include <stdio.h>
int main() {
char s1[100] = "ABCD";
const char* s2 = "ABCD";
printf("string : %s\n", s1);
printf("string : %s\n", s2);
}
#include <stdio.h>
int main() {
char* s1 = NULL;
const char* s2 = NULL; //const = ์์
char c = 'A';
s1 = &c;
s2 = &c;
*s1 = 'Z';
// *s2 = 'Z'; const๋ ์ฝ๊ธฐ๋ง ๊ฐ๋ฅ, ์ฐ๊ธฐ ์๋จ
printf("%c %c %c\n", *s1, *s2, c);
}
#include <stdio.h>
int main() {
char buf[10] = "ABC"; // R.W ์ฝ๊ธฐ ์ฐ๊ธฐ
char* s1 = buf; // R.W ์ฝ๊ธฐ ์ฐ๊ธฐ
const char* s2 = buf; // R ์ฝ๊ธฐ
s1[1] = 'Z';
s2[1] = 'Z'; // ์๋จ
printf("%s %s %s\n", s1, s2, buf);
}
#include <stdio.h>
int main() {
char buf[100];
printf("๋ฌธ์์ด ์
๋ ฅ: ");
gets_s(buf, 100);
printf("string : %s\n", buf);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
//strcpy(), strcat(), strcmp(), strlen()
char buf[100];
const char* s = "Hello!";
strcpy(buf, s); // ๋ฌธ์์ด์ ๋ณต์ฌํ๋ ๊ฒ ์ถ๋ ฅํ๋ฉด Hello!, Hello!
//strcpy(&buf[0], s);
printf("%s, %s\n", buf, s);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
//strcpy(), strcat(), strcmp(), strlen()
char buf[100];
const char* s = "Hello!";
strcpy(buf+1, s); //๋ฌธ์์ด 0๋ง๊ณ 1๋ถํฐ ์์ 0์ ๋น์๋ ์ถ๋ ฅํ๋ฉด HHello!, Hello!
buf[0] = 'H';
printf("%s, %s\n", buf, s);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
//strcpy(), strcat(), strcmp(), strlen()
char buf[100] = "ABC";
const char* s = "Hello!";
strcat(buf, s); //buf์ ๋์ s๋ฅผ ๋ถ์ด๋ ํจ์, ์ถ๋ ฅํ๋ฉด ABCHello, Hello
printf("%s, %s\n", buf, s);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
//strcpy(), strcat(), strcmp(), strlen()
char buf[100] = "ABC";
const char* s = "Hello!";
strcat(buf, s);
printf("len : %d\n", strlen(buf)); // ๋ฌธ์ ๊ฐ์๋ฅผ ์๋ ค์ค ์ถ๋ ฅํ๋ฉด len : 9 len : 6
printf("len : %d\n", strlen(s));
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
//strcpy(), strcat(), strcmp(), strlen()
const char* s1 = "ABC";
const char* s2 = "abc";
printf("%d\n", strcmp(s1, s2)); // ๋ ๋ฌธ์์ด์ **์ฌ์ ์์(์ํ๋ฒณ ์์)**๋ก ๋น๊ตํ๋ ํจ์
0: ๋ ๋ฌธ์์ด์ด ๊ฐ์
์์ (>0): ์ฒซ ๋ฒ์งธ ๋ฌธ์์ด์ด ์ฌ์ ์์ผ๋ก ๋ ํผ (๋์ค์ ์ด)
์์ (<0): ์ฒซ ๋ฒ์งธ ๋ฌธ์์ด์ด ์ฌ์ ์์ผ๋ก ๋ ์์ (๋จผ์ ์ด)
printf("%d\n", strcmp(s2, s1));
printf("%d\n", strcmp("12345", "12345"));
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
while (1) {
char buf[100];
printf("๋ฌธ์์ด ์
๋ ฅ :");
gets_s(buf,100);
//if (buf == "exit")->์๋จ, ์ฃผ์๋น๊ต
if (buf[0] == "exit"[0] &&
buf[1] == "exit"[1] &&
buf[2] == "exit"[2] &&
buf[3] == "exit"[3] &&
buf[4] == "exit"[4]
)
break;
printf("string : %s\n",buf);
}
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
while (1) {
char buf[100];
printf("๋ฌธ์์ด ์
๋ ฅ :");
gets_s(buf, 100);
//if (buf == "exit")->์๋จ, ์ฃผ์๋น๊ต
if (strcmp(buf, "exit") == 0)
break;
printf("string : %s\n", buf);
}
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
int main() {
char* sarr[1000] = { 0 };
int countStr = 0;
while (1) {
char buf[100];
printf("๋ฌธ์์ด ์
๋ ฅ :");
gets_s(buf, 100);
if (strcmp(buf, "exit") == 0)
break;
else {
printf("string : %s\n", buf);
sarr[countStr++] = buf;
}
}
for (int i = 0; i < countStr; ++i)
printf("[%d] : %s\n", i, sarr[i]);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char* sarr[1000] = { 0 };
int countStr = 0;
while (1) {
char buf[100];
printf("๋ฌธ์์ด ์
๋ ฅ :");
gets_s(buf, 100);
if (strcmp(buf, "exit") == 0)
break;
else {
printf("string : %s\n", buf);
char* s = (char*)malloc(strlen(buf) + 1);
strcpy(s, buf);
sarr[countStr++] = s;
}
}
for (int i = 0; i < countStr; ++i)
printf("[%d] : %s\n", i, sarr[i]);
for (int i = 0; i < countStr; ++i)
free(sarr[i]);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char* sarr[1000] = { 0 };
int countStr = 0;
int run = 1;
while (run) {
char buf[100];
printf("๋ฌธ์์ด ์
๋ ฅ :");
gets_s(buf, 100);
if (strcmp(buf, "exit") == 0)
run = 0;
else {
printf("string : %s\n", buf);
char* s = (char*)malloc(strlen(buf) + 1);
strcpy(s, buf);
sarr[countStr++] = s;
}
}
for (int i = 0; i < countStr; ++i)
printf("[%d] : %s\n", i, sarr[i]);
for (int i = 0; i < countStr; ++i)
free(sarr[i]);
}
#include <stdio.h>
struct Person {
char name[20];
int age;
};
int main() {
Person p1 = { " ํ๊ธธ๋ ", 30 };
printf("name : %s, age : %d\n", p1.name, p1.age);
}
#include <stdio.h>
struct Person {
//char name[20]; //0
char* name; //0 => ๋ฆฌํฐ๋ด ๋ฌธ์์ด์ ์ ์ฅ x
int age;
};
int main() {
struct Person p1 = { " ํ๊ธธ๋ ", 30 };
printf("name : %s, age : %d\n", p1.name, p1.age);
}