Effective C 2장

민거홍·2021년 5월 27일
0

Effective C

목록 보기
2/2

Chapter 2: Objects, Functions, Types

1. Objects, Functions, Types, and Pointers

  • An object is a storage in which you can represent values. More specifically speaking, it is a "region of data storage in the execution environment, and the contents of which can represent values." An object can be interpreted as having a particular type. A variable is an object.
  • A variable has a declared type that interprets the value of the object. The type is important as the collection of bits can be interpreted differently based on its type.
    - i.e. 1 in float (IEEE754) is 0x3f800000, but if this bit pattern is interpreted as an int, it is now a different number (1,065,353,216).
  • A function is not an object, but it does have types. Its type is characterized by both its return type and its parameter types.

2. Declaring Variables

  • When we are declaring variables, we assign it a type and provide a name (identifier), by which to reference a value.
  • i.e. int aVariable = 5;

3. Declaring multiple variables (in a single line)

  • We can declare multiple variables in a single declaration(line), but it may be very confusing if the variables' types are pointers or arrays.
//one is type char* (src), and another is type char (c)
char *src, c;

//one is type int (x), one is type int[5], and one is type int[4][3] (y)
int x, y[5], z[4][3];

Exercise 2.2 (함수 포인터)

  • 함수 포인터 배열을 정의 후 함수 포인터 배열에서 함수 호출하기
  • 함수 포인터는 다음과 같이 정의된다.
//함수형 (포인터명) (함수 파라미터)
//예시:
void hello(void); //함수 프로토타입
void (*helloFuncPtr)(void); //함수 포인터

int add (int, int);
int (*addFuncPtr)(int, int);

솔루션 code snippet. 코드 완성본은 github 참조해주세요!

profile
프로그래밍과 교육을 좋아하는 사람입니다.

0개의 댓글