✔️ 포인터의 기본 c
#include <stdio.h>
#pragma warning (disable : 4996)
int main(void)
{
int a = 5;
double d = 3.54;
int* p1 = &a;
double *p2 = &d;
printf("%d\n", a);
printf("%p\n", p1);
printf("%p\n", &a);
printf("%d\n", *p1);
printf("%f\n", *p2);
return 0;
}
출력 값:
5
0x7ff7bfeff238
0x7ff7bfeff238
5
3.540000
**주소값은 달라진다**
✔️ 배열과 포인터 관계 c
#include <stdio.h>
#pragma warning (disable : 4996)
int main(void){
int a[5] = {10, 20, 30, 40, 50};
int* p;
printf("%p\n", a);
printf("%p\n", &a[0]);
p = a;
return 0;
}
#include <stdio.h>
#pragma warning (disable : 4996)
void fun(int* p)
{
printf("%d\n", p[0]);
printf("%d\n", *(p + 0));
}
int main(void)
{
int a[5] = {10, 20, 30, 40, 50};
fun(a);
return 0;
}
출력 값;
10
10
✔️ 이중 포인터 c
#include <stdio.h>
#pragma warning (disable : 4996)
void fun(int* p)
{
printf("%d %d %d %d %d\n", p[-2], p[-1], p[0], p[1], p[2]);
printf("%d %d %d %d %d\n", *(p - 2), *(p - 1), *(p + 0), *(p + 1), *(p + 2));
}
void dummy(double* p)
{
printf("%.1f %.1f %.1f\n", p[0], p[1], p[2]);
printf("%.1f %.1f %.1f\n", *(p + 0), *(p + 1), *(p + 2));
}
int main(void)
{
int a[5] = {10, 20, 30, 40, 50};
double d[3] = {1.1, 2.2, 3.3};
fun(a+2);
dummy(d);
return 0;
}
출력 값:
10 20 30 40 50
10 20 30 40 50
1.1 2.2 3.3
1.1 2.2 3.3
#include <stdio.h>
#pragma wanring( disable : 4996)
int main(void)
{
int a = 5;
int* p = &a;
int** p2 = &p;
int*** p3 = &p2;
printf("%d\n", a);
printf("%d\n", *p);
printf("%d\n", **p2);
printf("%d\n", ***p3);
return 0;
}
출력 값:
5
5
5
5
✔️ 문자열과 포인터 c
#include <stdio.h>
#pragma warning (disable : 4996)
int main()
{
int a = 5;
char c = 'R';
char* p = "Hello World";
char* p2 = "odd";
printf("%s\n", p);
printf("%s\n", p2);
return 0;
}
출력 값:
Hello World
odd
#include <stdio.h>
#pragma warning (disable : 4996)
int main()
{
int a = 5;
char c = 'R';
char* p = "Hello World";
char* p2 = "odd";
printf("%s\n", p);
printf("%s\n", p2);
printf("%c\n", p[1]);
printf("%c\n", *(p + 1));
printf("\n 한 글자씩 문자열 출력\n");
int i;
for (i = 0; i < 5; i++)
{
printf("%c ", *(p + i));
}
return 0;
}
출력 값:
Hello World
odd
e
e
한 글자씩 문자열 출력
H e l l o
✔️ 포인터 배열 c
#include <stdio.h>
#pragma warning (disable : 4996)
void fun(int** p2)
{
printf("포인터 배열을 전달 받을 시 이중 포인터로 받는다.\n");
printf("b의 값을 출력합니다 : %d\n", *p2[0]);
}
int main()
{
int* p[5] = { NULL };
int a = 5, b = 10;
p[2] = &a;
printf("%d\n", *p[2]);
p[0] = &b;
printf("%d\n", *p[0]);
fun(p);
return 0;
}
출력 값:
5
10
포인터 배열을 전달 받을 시 이중 포인터로 받는다.
b의 값을 출력합니다 : 10
✔️ 문자열과 포인터배열 c
#include <stdio.h>
#pragma warning (disable :4996)
void displayFruit(const char** fruit)
{
printf("***문자열 단위로 과일명 출력***\n");
for(int i = 0; i < 5; i++)
{
printf("%s\n", fruit[i]);
}
printf("***문자열 단위로 과일명 출력***\n");
for (int i = 0; i < 5; i++)
{
for(int j = 0; *(fruit[i] + j) != '\0'; j++)
{
printf("%c\n", *(fruit[i] + j));
}
puts("");;
}
}
int main(void)
{
/* const char* s1 = "apple";
const char* s2 = "banana";
const char* s3 = "orange";
const char* s4 = "strawberry";
const char* s5 = "pear";*/
const char* fruit[5] = {
"apple",
"banana",
"orange",
"strawberry",
"pear"
};
displayFruit(fruit);
for(int i = 0; i < 5; i++)
{
printf("%s\n", fruit[i]);
}
return 0;
}
출력 값 :
***문자열 단위로 과일명 출력***
apple
banana
orange
strawberry
pear
***문자열 단위로 과일명 출력***
a
p
p
l
e
b
a
n
a
n
a
o
r
a
n
g
e
s
t
r
a
w
b
e
r
r
y
p
e
a
r
apple
banana
orange
strawberry
pear
✔️ 짧은 글 연습 프로그램 c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#pragma warning (disable :4996)
#define ANSWER_LEN 255
int main(void)
{
const char* str[10] = {
"All greatness is unconscious, or it is little and naught",
"Life is an incurable disease",
"Variety's the very spice of life",
"No man is the wiser for his learning",
"Love is a sickness full of wees, all remedies refusing",
"Be great in act, as you have been in thought",
"A rose is sweeter in the bud than full blown",
"Genius must be born, and never can be taught",
"Custom reconciles us to everything",
"The happiest women, like the happiest nations, have no history"
};
int no;
char answer[ANSWER_LEN];
char isExit;
int correct;
clock_t begin, end;
srand((unsigned int)time(NULL));
do
{
no = rand() % 10;
printf("%s\n", str[no]);
begin = clock();
fgets(answer, ANSWER_LEN, stdin);
end = clock();
correct = 0;
for(int i = 0; answer[i] != '\0'; i++)
{
if(answer[i] == str[no][i])
{
++correct ;
}
}
printf("%d", correct);
printf("\n\n\t\t정확도 : %.2f", (double)correct / strlen(str[no])/100);
printf("입력 시간 : %.3f\n", ((double)end - begin)/ CLOCKS_PER_SEC);
printf("\n\t\t계속(아무키) / 종료(Q/q)");
getchar();
isExit = getchar();
} while(isExit != 'Q' && isExit != 'q');
return 0;
}
✔️ void 포인터
#include <stdio.h>
#pragma warning (disable : 4996)
int main(void)
{
int a = 5;
int* p = &a;
double d = 3.5;
double* pD = &d;
printf("%d\n", *p);
printf("%f", *pD);
void* pV;
pV = &a;
printf("%d\n", *(int*)pV);
pV = &d;
printf("%f\n", *(double*)pV);
return 0;
}
출력 값 :
5
3.500000
5
3.500000