2014-2 κΈ°μ΄νλ‘κ·Έλλ° 2
19.1μ°¨μ ν¬μΈν°μ λ°°μ΄
μ 1μ₯
1μ°¨μ ν¬μΈν°μ λ°°μ΄ 1.1
ν¬μΈν°μ λ°°μ΄μ μ°¨μ΄μ
λ€μ νλ‘κ·Έλ¨μ μ€λ₯λ₯Ό μ€λͺ νκ³ μμ ν΄λ³΄μ. 1.1.0 물리μ μ£Όμλ μμ 1 2
int main ( )
3
{
4
char carr[3] ={βAβ, βBβ, βCβ};
5
char *cp = carr;
6
printf("%d %d\n", sizeof(cp), sizeof(carr) );
7 8
cp = NULL;
9
carr = NULL;
10 11
}
12
//carr = NULL; //μμλ‘ λμ λΆκ°. error C2106: β=β : left operand must be l-value
1.1.1
error C2106: β=β : left operand must be l-value
μμμ κ°μ λ£μΌλ €κ³ νλ νλ 1.1.1 μμμ κ°μ λ£μΌλ €κ³ νλ νλ μ 1
#include <stdio.h>
2
#define NUM 3
3
void main ( )
4
{
5
char* a[4]={"kim", "choi", "park", "jung"};
6
char b[4][5]={"kim", "choi", "park", "jung"};
7
NUM = 5; // error C2106: β=β : left operand must be l-value
8
a[2]=a[3];
9
b[2]=b[3]; //error C2106: β=β : left operand must be l-value ??
10 11
}
2
ν¬μΈν°μ λ°°μ΄μ μ°¨μ΄μ
1.1.2
ν¬μΈν°λ λ³μ? λ°°μ΄μ μμ ?
Figure 1.1: ν¬μΈν°λ λ³μ
Figure 1.2: λ°°μ΄μ μμ
3
μ 1 μ₯ 1μ°¨μ ν¬μΈν°μ λ°°μ΄
1.1.3
μ 리 ν΄μΌ νλ λͺ κ°μ§ Error
1.1.2 pAλ μ°λ κΈ° κ°μΌλ‘ μ΄κΈ°ν λ¨ 1
int main ( )
2
{
3
int *pA;
4
*pA=10; return 0;
5 6
}
7
1.1.3 μ£Όμμμμ§μ λΆκ° Compile Time Error !! 1
int main ( )
2
{
3
int *pA = 0x00123456;
4
*pA=10; return 0;
5 6
}
1.1.4 p[0] = βLβ; μμ μμ μ κ·Ό μλ Run-time Error!! 1
int main ( )
2
{
3
char a[4] = "KIM";
4
char *p = "KIM";
5
p[0] = βLβ;
6
a[0] = βLβ;
7
}
8
1.1.5 OK !! 1
int main ( )
2
{
3
char a[4] = "KIM";
4
char *p = a;
5
p[0] = βLβ; a[0] = βLβ;
6 7 8
4
}
λ°°μ΄κ³Ό ν¬μΈν° μ΄λ¦μ νμ© arr[i] = *(arr+i)
1.2
λ°°μ΄κ³Ό ν¬μΈν° μ΄λ¦μ νμ© arr[i] = *(arr+i)
λ°°μ΄ μ΄λ¦μ ν¬μΈν°μ²λΌ, ν¬μΈν°λ³μ μ΄λ¦μ λ°°μ΄ μ΄λ¦μ²λΌ νμ© νλκ²μ΄ κ°λ₯ ν¨
Figure 1.3: λ°°μ΄κ³Ό ν¬μΈν° μ΄λ¦μ νμ©
1.2.0 ν¬μΈν°μ λ°°μ΄μ κ΄κ³ 1
int main ( )
2
{
3
int arr[3] = {0,1,2};
4
int *ptr = arr;
5
printf("%d %d %d \n", *(ptr+0),*(ptr+1),*(ptr+2));
6
printf("%d %d %d \n", ptr[0],ptr[1],ptr[2]);
7 8
}
9
---------- Run GCC ----------
10
0 1 2
11
0 1 2
5
μ 1 μ₯ 1μ°¨μ ν¬μΈν°μ λ°°μ΄
ν¬μΈν° μ°μ°
1.3 1.3.1
λ°°μ΄κ³Ό ν¬μΈν° μ°μ°μ μλ―Έ μ°¨μ΄
β’ ν¬μΈν°λ μ£Όμμ΄λ―λ‘ κΈ°λ³Έμ μΌλ‘ +- μ°μ°μ΄ κ°λ₯νλ€. β’ νμ§λ§ μ£Όμλ₯Ό λ³κ²½νκ³ μ ν κ²½μ°λ 물리μ μ£ΌμμΈκ²½μ°(λ©λͺ¨λ¦¬μ μ£Όμ)λ λ³κ²½μ΄ λΆκ°λ₯νκ³ , ν¬μΈν°λ³ μμ κ°μ μ£Όμλ³μμ΄λ―λ‘ λ³κ²½ κ°λ₯νλ€. β’ λνμ μΈ μλ‘ λ°°μ΄μ μ΄λ¦μ 물리μ μ£Όμ μ΄λ―λ‘ λ³κ²½ λΆκ°λ₯νλ€.
1.3.0 λ°°μ΄μ μ΄λ¦μ 물리μ μ£Όμμ΄λ―λ‘ λ³κ²½ λΆκ°λ₯ 1
int main ( )
2
{ int arr[] = {1};
3
arr++; //error
4 5
}
1.3.1 μ¦κ°μ°μ°μμ μλͺ»λ μ¬μ© 1
int main ( )
2
{
3
int arr1[2];
4
char arr2[2];
5
double arr3[2];
6 7
printf("%d λ²μ§, %d λ²μ§, %dλ²μ§\n", arr1++,arr2++,arr3++); //error
8
printf("%d λ²μ§, %d λ²μ§, %dλ²μ§\n", arr1+1,arr2+1,arr3+1); //ok
9 10 11
6
}
ν¬μΈν° μ°μ°
1.3.2
ν¬μΈν° μ°μ°μ ν΅ν λ°°μ΄ μμμ μ κ·Ό
1.3.2 μ¦κ°μ°μ°μμ μ΄μ©ν λ°°μ΄ κ°μ λ³κ²½ 1
int main ( )
2
{
3
int arr[] = {1,2,3,4,5};
4
int* parr = arr;
5
printf("%d \n", *parr);
6
printf("%d \n", *(++parr));
7
printf("%d \n", *(++parr));
8
printf("%d \n", *(parr+1)); printf("%d \n", *(parr+2));
9 10
}
11
---------- Run GCC ----------
12
1
13
2
14
3
15
4
16
5
1.3.3 μ°μ°μ μ°μ μμλ₯Ό μ΄μ©ν λ°°μ΄ κ°μ λ³κ²½ 1
int main ( )
2
{
3
int arr[] = {10,20,30,40,50};
4
int* parr = arr;
5
printf("%d \n", *parr++);
6
printf("%d \n", (*parr)++);
7
printf("%d \n", *parr);
8
printf("%d \n", *parr+1);
9
printf("%d \n", *(parr+1)); printf("%d \n", *(parr+2));
10 11
}
12
---------- Run GCC ----------
13
10
14
20
15
21
16
22
17
30
18
40
7
μ 1 μ₯ 1μ°¨μ ν¬μΈν°μ λ°°μ΄
1.3.4 μ°μ°μ μ°μ μμλ₯Ό μ΄μ©ν λ°°μ΄ κ°μ λ³κ²½ 1
int main ( )
2
{
3
int arr[] = {10,20,30,40,50};
4
int* parr = arr;
5
printf("%d \n", *(parr=parr+2));
6
printf("%d \n", *(parr++));
7
printf("%d \n", parr[-2]);
8
printf("%d \n", *(parr+1)); printf("%d \n", *(++parr));
9
8
10
}
11
---------- Run GCC ----------
12
30
13
30
14
20
15
50
16
50
λ¬Έμμ΄ μμλ₯Ό κ°λ¦¬ν€λ ν¬μΈν°
λ¬Έμμ΄ μμλ₯Ό κ°λ¦¬ν€λ ν¬μΈν°
1.4 1.4.1
λ¬Έμμ΄ νν λ°©μμ μ΄ν΄
1.4.0 λ°°μ΄κΈ°λ°μ λ¬Έμμ΄ λ³μ 1
void main(void)
2
{ char str[10]="Hello";
3 4
}
1.4.1 ν¬μΈν° κΈ°λ°μ λ¬Έμμ΄ μμ 1
void main(void)
2
{ char *str="Hello";
3 4
}
1.4.2 λ°°μ΄ ν¬μΈν° κΈ°λ°μ λ¬Έμμ΄ λ³μ 1
void main(void)
2
{ char tmp[] = "Hello";
3
char *str=tmp;
4 5
}
1.4.3 λ¬Έμμ΄μ²λ¦¬ ν¨μ(strcpy)λ₯Ό μ΄μ©ν λ¬Έμμ΄ λ³μ 1
int main(void)
2
{
3
char tmp[10];
4
strcpy(tmp,"Hello"); return 0;
5 6
}
9
μ 1 μ₯ 1μ°¨μ ν¬μΈν°μ λ°°μ΄
1.4.4 Heap λ©λͺ¨λ¦¬μ λ¬Έμμ΄μ²λ¦¬ ν¨μλ₯Ό μ΄μ©ν λ¬Έμμ΄ λ³μ 1
int main(void)
2
{
3
char* tmp;
4
tmp = (char*)malloc(10);
5
strcpy(tmp,"Hello");
6
return 0;
7
}
1.4.5 μμμ ν¬ν¨ν λ¬Έμμ΄ μμ± λ°©λ² sprintf() νμ© 1
#include <stdio.h>
2
#include <stdlib.h>
3 4
void main(int argc, char* argv[])
5
{
6
char str[50];
7
sprintf(str,"Hello %s\n","sprintf()"); printf("%s",str);
8 9
10
}
λ°°μ΄κ³Ό ν¬μΈν°μ μμ© λ κ°μ§
1.5 1.5.1
λ°°μ΄κ³Ό ν¬μΈν°μ μμ© λ κ°μ§ ν¬μΈν° λ°°μ΄
β’ λ°°μ΄μ μμλ‘ ν¬μΈν°λ₯Ό μ§λλ λ°°μ΄ β’ λ°°μ΄μ μ€μ λ΄κΈ°λ κ°μ? μ£Όμ
1.5.0 μ£Όμκ°μ λ΄λ λ°°μ΄μ λͺ¨μ΅ 1
#include <stdio.h>
2 3
int main(void)
4
{
5
int a=10, b=20, c=30;
6
int* arr[3]={&a, &b, &c};
7 8
printf("%d \n", *arr[0]);
9
printf("%d \n", *arr[1]);
10
printf("%d \n", *arr[2]);
11
return 0;
12 13
}
14
---------- Run GCC ----------
15
10
16
20
17
30
1.5.1 ν¬μΈν° λ°°μ΄μ μ΄μ©ν 볡μμ λ¬Έμμ΄ λ΄κΈ° μ 1
#include <stdio.h>
2 3
#include <stdio.h>
4 5
int main(void)
6
{
7
char* arr[3]={
8
"Hello World",
9
"C Programming",
10
"java Programming"
11
};
12
printf("%s \n", arr[0]);
13
printf("%s \n", arr[1]);
14
printf("%s \n", arr[2]);
15
return 0;
16 17
}
18
---------- Run GCC ----------
19
Hello World
20
C Programming
21
java Programming
11
μ 1 μ₯ 1μ°¨μ ν¬μΈν°μ λ°°μ΄
1.5.2
λ°°μ΄ ν¬μΈν°
β’ λ°°μ΄μ κ°λ¦¬ν€λ ν¬μΈν° β’ int (* arr1)[10];
12
μ°μ΅λ¬Έμ
μ°μ΅λ¬Έμ
1.6
Question
1
μλ νλ‘κ·Έλ¨μ λμμ λ©λͺ¨λ¦¬λ§΅μ κ·Έλ €μ μ€λͺ ν΄λ³΄μμ€. λ λ²μ§Έ νλ‘κ·Έλ¨μ Run-time error κ° λ°μνλ€ μμΈμ μ€λͺ νμμ€. 1.6.0 λ¬Έμ
1
(1)
2
void main(void)
3
{
4
char str[100];
5
scanf("%s",str); printf("%s",str);
6 7
}
8
(2)
9
void main(void)
10
{
11
char *str;
12
scanf("%s",str); printf("%s",str);
13 14
}
μ λ΅ 1.6.1 fgets μ λ΅ 1
#include <stdio.h>
2
2
λ€μ νλ‘κ·Έλ¨μ μλ¬ μμΈμ λ©λͺ¨λ¦¬λ§΅μ κ·Έλ €μ μ€λͺ νμμ€ 1.6.2 λ¬Έμ
1
int main()
2
{
3
char str1[5]="abcd";
4
char *str2="ABCD";
5 6
printf("%s \n", str1);
7
printf("%s \n", str2);
8 9
str1[0]=βxβ;
10
str2[0]=βxβ;
11 12
printf("%s \n", str1);
13
printf("%s \n", str2);
13
μ 1 μ₯ 1μ°¨μ ν¬μΈν°μ λ°°μ΄ 14
return 0;
15 16
}
μ λ΅ 1.6.3 fgets μ λ΅ 1
#include <stdio.h>
2
3
μλνλ‘κ·Έλ¨μμ βHello WorldβμβWβλ§ μΆλ ₯ν΄λ³΄μμ€ 1.6.4 λ¬Έμ
1
int main(void)
2
{
3
char* arr[3]={
4
"Hello World",
5
"C Programming",
6
"java Programming"
7
};
8
printf("%c\n", ______________________); return 0;
9 10
}
μ λ΅ 1.6.5 fgets μ λ΅ 1
#include <stdio.h>
2
int main(void)
3
{
4
char* arr[3]={
5
"Hello World",
6
"C Programming",
7
"java Programming"
8
};
9
printf("%c\n", *(arr[0]+6)); printf("%c\n", arr[0][6]);
10
return 0;
11 12
4
}
λ€μ νλ‘κ·Έλ¨μ λ©λͺ¨λ¦¬λ§΅κ³Ό μ€νκ²°κ³Όλ₯Ό μμ±νμμ€. 1.6.6 λ¬Έμ
14
μ°μ΅λ¬Έμ
1
int main(int argc, char* argv[])
2
{
3
int arr[5] = {9,8,7,6,5};
4
int *pt =arr;
5
int i;
6 7
printf("%d\n",*pt+3);
8
printf("%d\n",*(pt+=3));
9 10
printf("%d\n",*pt++);
11
printf("%d\n",*pt);
12 13
printf("%d %d %d\n",*pt<<2, *pt, (*pt)++);
14
printf("%d\n",*pt);
15
printf("%d %d\n",pt[-1],*(arr+2));
16 17
}
μ λ΅ 1.6.7 fgets μ λ΅ 1
int main(int argc, char* argv[])
2
{
3
int arr[5] = {9,8,7,6,5};
4
int *pt =arr;
5
int i;
6 7
printf("%d\n",*pt+3);
8
printf("%d\n",*(pt+=3));
9 10
printf("%d\n",*pt++);
11
printf("%d\n",*pt);
12 13
printf("%d %d %d\n",*pt<<2, *pt, (*pt)++);
14
printf("%d\n",*pt);
15
printf("%d %d\n",pt[-1],*(arr+2));
16
5
17
}
18
---------- Run GCC ----------
19
12
20
6
21
6
22
5
23
24 6 5
24
6
25
6 7
λ€μ νλ‘κ·Έλ¨μ λ©λͺ¨λ¦¬λ§΅κ³Ό μ€νκ²°κ³Όλ₯Ό μμ±νμμ€. 1.6.8 λ¬Έμ
15
μ 1 μ₯ 1μ°¨μ ν¬μΈν°μ λ°°μ΄ 1
int main(int argc, char* argv[])
2
{
3
char carr[] = "ABCDEFGHI";
4
char *pt = carr + 2;
5
char *pt2 = pt + 2;
6
printf("%x \n",*pt2+=3);
7 8
printf("%d \n",*(pt+=1));
9 10
printf("%x \n",*(--pt));
11 12
printf("%c \n",*pt++);
13 14
printf("%d %d %d \n",*pt<<1, *pt--, (*pt)++);
15 16
printf("%d \n",(*pt2 ^ *pt++));
17 18
printf("%c %d \n",pt[-1],*(carr-2));
19 20
}
μ λ΅ 1.6.9 fgets μ λ΅ 1
int main(int argc, char* argv[])
2
{
3
char carr[] = "ABCDEFGHI";
4
char *pt = carr + 2;
5
char *pt2 = pt + 2;
6
printf("%x \n",*pt2+=3);
7 8
printf("%d \n",*(pt+=1));
9 10
printf("%x \n",*(--pt));
11 12
printf("%c \n",*pt++);
13 14
printf("%d %d %d \n",*pt<<1, *pt--, (*pt)++);
15 16
printf("%d \n",(*pt2 ^ *pt++));
17 18
printf("%c %d \n",pt[-1],*(carr-2));
19 20
}
21
16
22
48
23
68
24
43
25
C
26
134 69 68
27
11
28
C 126
μ°μ΅λ¬Έμ
6
λ€μ νλ‘κ·Έλ¨μ μμμ μ λ ₯ λ°μ μ°μ°νλ νλ‘κ·Έλ¨μ΄λ€. μ¬μ©μ μ μν¨μ calculator() λ₯Ό μμ± νμμ€ 1.6.10 λ¬Έμ
1
float calculator(char*);
2 3
int main(int argc, char* argv[])
4
{
5
float result=0;
6
char str[1024];
7
printf("μμμ μ λ ₯νμμ€. :");
8
scanf("%s",str);
9
while(1){
10
result = calculator(str);
11
printf("%s = %f\n", str,result);
12
printf("μμμ μ λ ₯νμμ€. :");
13
scanf("%s",str); }
14
return 0;
15 16
}
17 18
μμμ μ λ ₯νμμ€. :4+3
19
4+3 = 7.000000
20
μμμ μ λ ₯νμμ€. :4-3
21
4-3 = 1.000000
22
μμμ μ λ ₯νμμ€. :4*3
23
4*3 = 12.000000
24
μμμ μ λ ₯νμμ€. :4/3
25
4/3 = 1.000000
26
μμμ μ λ ₯νμμ€. :
μ λ΅ 1.6.11 fgets μ λ΅ 1
#include <stdio.h>
2 3
float calculator(char*);
4 5
int main(int argc, char* argv[])
6
{
7
float result=0;
8
char str[1024];
9
printf("μμμ μ λ ₯νμμ€. :");
10
scanf("%s",str);
11
while(1){
12
result = calculator(str);
13
printf("%s = %f\n", str,result);
14
printf("μμμ μ λ ₯νμμ€. :"); scanf("%s",str);
15 16
}
17
return 0;
17
μ 1 μ₯ 1μ°¨μ ν¬μΈν°μ λ°°μ΄ 18
}
19
float calculator(char* str)
20
{
21
float result=0;
22
int a=0,b=0;
23
int a_reg=1,b_reg=1;
24
char op;
25
while(*str != 0)
26
{
27
if(a_reg && *str >= β0β && *str <= β9β)
28
{ a = *str - 48;
29
a_reg = 0;
30 31
}
32
else if(b_reg && *str >= β0β && *str <= β9β)
33
{ b = *str - 48;
34
b_reg = 0;
35 36
}
37
else if(*str == β+β || *str == β-β || *str == β*β || *str == β/β )
38
{ op = *str;
39
}
40
str++;
41 42
}
43
switch(op)
44
{ case β+β :
45
result = a + b;
46
break;
47
case β-β:
48
result = a - b;
49
break;
50
case β*β:
51
result = a * b;
52
break;
53
case β/β:
54
result = a / b;
55
break;
56 57
}
58
return result;
59
}
1.6.12 λ€λ₯Έλ°©λ² 1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <math.h>
4
#include <string.h>
5
18
6
float calculator(char* str)
7
{
8
int op_pos = 0;
9
int end_pos = strlen(str);
μ°μ΅λ¬Έμ
10
char op1[10],op2[10];
11
while(1)
12
{ if(str[op_pos] == β+β || str[op_pos] == β-β || str[op_pos] == β*β || str[op_pos] == β/β)
13
break;
14
else
15
op_pos++;
16 17
}
18
int i;
19
for(i=0; i<op_pos; i++) op1[i] = str[i];
20 21
op1[i] = β\0β;
22
i++;
// μ°μ°μ μ리μ 무μ
23 24
int j;
25
for(j=i; j<end_pos; j++)
26
op2[j-i] = str[j];
27
op2[j-i] = β\0β;
28 29
float result=0;
30
switch(str[op_pos])
31
{
32
case β+β :
33
result = atoi(op1) + atoi(op2);
34
break;
35
case β-β:
36
result = atoi(op1) - atoi(op2);
37
break;
38
case β*β:
39
result = atoi(op1) * atoi(op2);
40
break;
41
case β/β:
42
result = atoi(op1) / atoi(op2); break;
43
}
44
return result;
45 46
}
47 48 49
int main(int argc, char* argv[])
50
{
51
float result=0;
52
char str[1024];
53
printf("μμμ μ λ ₯νμμ€. :");
54
scanf("%s",str);
55
while(1){
56
result = calculator(str);
57
printf("%s = %f\n", str,result);
58
printf("μμμ μ λ ₯νμμ€. :"); scanf("%s",str);
59
}
60
return 0;
61 62
}
19