第2章Effective C


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);
    ソリューションコードフラグメント.コードの完全版はgithubを参照してください!