Problem 1: Casting
1
2
3
double a = 0;
double* b = &a;
double c = __a - *b;
- What shoud be filled in for the __?
- a.
*
- b.
&
- c. (nothing)
- b.
Problem 2: Casting
1
2
3
4
5
void f(int a, int* b, int* c)
{
__b = a * a;
*c = a * a * a;
}
- What shoud go in the __?
- a.
*
- b.
&
- c.
void
- d.
const*
- e. (nothing)
- b.
Problem 3: Initializing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void h(int const* vals, size_t n, int* even, int* odd)
{
__
__
for (size_t i = 0; i < n; i++>)
{
int* w;
if (vals[i] % 2)
w = even;
else
w = odd;
(*w)++;
}
}
- What shoud be filled in for the __ in the following code?
- a.
even = 0; odd = 0;
- b.
even = &0; odd = &0;
- c.
*even = 0; *odd = 0;
- d.
&even = 0; &odd = 0;
- b.
Problem 4: Casting
1
2
3
char* g(int const*, int);
char a;
int b;
- Which of the following would be legal ways to call the g function?
- a.
a = *g(*b, b);
- b.
&a = g(b, *b);
- c.
&a = g(*b, b);
- d.
a = *g(&b, b);
- e.
a = *g(b, *b);
- f.
&a = g(&b, b);
- b.
Answer
Question 1
The answer is c.
There should exist the same types of variables in each side of = operator / = 연산자의 양변에는 동일한 타입의 변수가 있어야 합니다.
In the line 3, the type of the left side of = operator is double
. So, the type of the right side of = operator should be double
. In that sense, __a - *b
should be double
type. We all know substraction bewtween double
s return double
. So, __a
and *b
should be double
type. b
is pointer of double
, so *b
returns double
. Finally, we just get __a
should have type double
. a
is already initialized as double
type, so we don’t need nothing in the blank.
Question 2
The answer is a.
*
in front of pointer totype
returnstype
value. /type
을 가리키는 포인터 앞에*
은type
값을 반환합니다.
As we already discussed, in the line 3, the type of the right side of = operator is int
. (Because int
* int
returns int
.) So, the type of the left side of = opertator should be int
. In that sense, __b
should be int
type. b
is pointer of int
, So we can expect *b
become the int
. So, the blank should be filled with *
.
Question 3
The answer is c.
When you initialize the value of pointer, use in way:
*ptr = value;
/ 포인터의 값을 초기화할 때에는,*ptr = value;
형식으로!
As we already discussed, in the line 3 and 4, the type of the right side of = operator is int
. So the type of the left side of = operator shoud also be int
. even
and odd
is pointer to int
type. So, we can change even
and odd
as type int
with *
in front of them.
These problems in this kind focus on the type of variables, so you don’t need to too focus and understand the logic of the code below. Just concentrate on the type and pointer of the variables. / 요런 문제들은 변수의 형식, 즉 타입에 집중하기 때문에 밑에 코드의 로직을 세세하게 살펴보지 않아도 괜찮다. 오로지 변수의 타입과 포인터에만 집중하자.
왜 b는 정답이 되지 못할까? 물론 = 연산자의 양변이
int
의 포인터로 동일하지만,&
연산자는 변수 앞에만 붙을 수 있기 때문에&0
은 성립되지 못한다.
Question 4
The answer is d.
&a =
is impossible because we cannot modify the value of address of variable which is already initialiezed. /&a =
는 불가능한 코드이다. 이미 지정된 변수의 주솟값을 고칠 수는 없기 때문이다.
First of all, the return type of g
is char*
. And a
is char
. So, we need to add *
in front of g
in order to unify the type
s between = operator. So, we can conclude the answer is a = *g(...)
form.
Now let’s look in the arguments. The types are int const*
and int
. The first one seems serious. But we can know that if there is *
in type, it is pointer whatever. So, int const*
is pointer of int
, and it need some address. So, we can easily notice that the argument should be (&b, b)
. Thus, the answer is a = *g(&b, b)
The End!