C++ Note(2), Pointer, Built-in Array, and C-style String

Pointers

  1. To declare a pointer, int* pint *p皆可,但是int* p, q表示p为指针,qint型变量,如果要在一行里定义多个指针,int *p, *q. Use this to initialize pointer int* aPtr = &a;

  2. Require to include letter Ptr in each pointer variable name to make it clear that variable is a pointer.

  3. nullptr is null pointer, initialize all pointers to prevent pointing to unknown or uninitialized areas of memory. Prior to C++11, 0 or NULL is equivalent ot nullptr.

  4. Dereferencing an uninitialized pointer results in undefined behavior that could cause a fatal execution-time error. This could also lead to accidentally modifying important data, allowing the program to run to completion, possibly with incorrect results.

  5. Dereferencing a null pointer results in undefined behavior and typically causes a fatal execution-time error. Ensure that a pointer is not null before dereferencing it.

  6. Operator precedence
    在这里插入图片描述

  7. Insight: Pass-By-Reference with a Pointer Actually Passes the Pointer By Value

Built-in Array

  1. Initializing Built-in Arrays. int n[5]{1, 2, 3, 4, 5}; If only give 4 elements, the rest of them are set to default value, 0 for int, false for bool, and nullptr for Pointer.

  2. arrayName is implicitly convertible to &arrayName[0].

  3. Applying the const type qualifier to a built-in array parameter in a
    function definition to prevent the original built-in array from being modified in the function body is another example of the principle of least privilege. Functions should not be given the capability to modify a built-in array unless it’s absolutely necessary.

  4. Built-in array do not know its size.

  5. Limitations of Built-in Arrays (IMPORTANT)

    1. They cannot be compared using the relational and equality operators – you must use a loop to compare two built-in arrays element by element.
    2. They cannot be assigned to one another – an array name is effectively a pointer that is const .
    3. They don’t know their own size -— a function that processes a built- in array typically receives both the built-in array’s name and its size as arguments.
    4. They don’t provide automatic bounds checking —- you must ensure that array-access expressions use subscripts that are within the built-in array’s bounds.
  6. In modern C++, should use array or vector objects to represent lists and tables of values. However, there are cases that we should use built-in array, for example, the command-line arguments.

  7. If a value does not (or should not) change in the body of a function to which it’s passed, the parameter should be declared const. For example int sumElements(const int* values, const size_t numberOfElements) or int sumElements(const int[] values, const size_t numberOfElements).

  8. object like array, vector are passed by value, the built-in array are passed by pointer.

  9. Use pass-by-value to pass fundamental-type arguments(int, double), unless the called function are required to be able to directly modify the value in the caller.

Using const with Pointers

There are four ways to pass a pointer to a function:

  1. a non-constant pointer to non-constant data,
  2. a non-constant pointer to constant data,
  3. a constant pointer to non-constant data
  4. a constant pointer to constant data

Non-constant Pointer to Non-constant Data

  • the data can be modified through the dereferenced pointer
  • and the pointer can be modified to point to other data
    Highest access is granted, e.g. int* countPrt.

Non-constant Pointer to Constant Data

  • a pointer that can be modified to point to any data of the appropriate type, but
  • the data to which it points cannot be modified through the pointer.

Such a pointer might be used to receive a built-in array argument to a function that should be allowed to read the elements, but not modify them. Any attempt to modify the data in the function result in a compilation error.
E.g. const int* countPtr;

Read from right to left, countPtr is a pointer to an integer constant.

Also, int const* countPtr;

const applies to the int, not the pointer. Read from right to left, countPtr is a pointer to a constant integer.

Constant Pointer to Non-constant Data

  • always points to the same memory location, and
  • the data at that location can be modified through the pointer.

Pointer that are declared const must be initialized when they are declared, but if the pointer is a function parameter, it’s initialized with the pointer that’s passed to the function.
E.g. int* const countPtr;,

Read from right to left, countPtr is a constant pointer to a non-constant integer.

Constant Pointer to Constant Data

Minimum access privilege.

  • such a pointer always points to the same memory location, and
  • the data at that location cannot be modified via the pointer.

This is how a built-in array should be passed to a function that only reads from the array, using array subscript notation, and does not modify it.
E.g. const int* const ptr{&x};

Pointer Arithmetic

  • Pointer arithmetic is meaningful only on a pointer that points to a built-in array.

  • We cannot assume that two variables of the same type are stored contiguously in memory unless they’re adjacent elements of a built-in array.

  • A pointer can be assigned to another pointer if both pointers are of the same type. Except that void* is a generic pointer capable of representing any pointer.

  • Different pointer assignment need reinterpret_cast.

  • Cannot dereference a void*.

  • Pointer comparisons compare the addresses stored in the pointers.

  • Pointer value is 0 means nullptr.

  • Array name a declared as int a[] is const, cannot change, like a += 3 is not permitted. *(a + 3), and a[3] means the third element in array.

Pointer Based String

  1. character constants, single quota. string literals, string constants use double quota.

  2. String literal initializers

    char color[]{"blue"};
    const char* colorPtr{"blue"};
    const char[] color{'b', 'l', 'u', 'e', '\0'};
    
  3. String literals exist for the duration of the program and may be shared if the same string literal is referenced from multiple locations in a program. String literals cannot be modified.

  4. Not allocating sufficient space in a built-in array of char s to store the null character that terminates a string is a logic error.

  5. Creating or using a C string that does not contain a terminating null character can lead to logic errors.

  6. Reading and displaying c-style strings.

  7. One common error

    void func() {
    	const char c[]{'1', '2', '3'};
    	cout << "in func(), c = " << c << endl;
    }
    int main() {
    	func();
    }
    

    The output will start with 123 and end with the '\0' in the memory.

    in func(), c = 123@iZ��
    

    This is because the miss of \0 in the char array. We use initialize a char array with \0 to terminate the string.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值