C++ Primer 总结之Chap4 Arrays and Pointers

本系列为本人温习C++基础时所记的tips,欢迎各位同学指正,共同进步TvT。

Modern C++ programs should almost always use vector and iterator in preference to the lower-lever array and pointer.

int ia[5] = {1, 1, 1}; // {1, 1, 1, 0, 0}
char ch1[] = {'d'}; // no null character('\0')
char ch2[] = "d"; // null terminator added automatically
cout << ch1 << endl; // error happens
char ch3[3] = "aaa"; // wrong, size should be >= 4
  1. A non-const variable or a const variable whose value is not known until run time, cannot be used to specify the dimension of an array.

  2. For built-in type, variables defined outside the body of a function, or say, global variables, are initialized to zero. Local variables are uninitialized. For class type, no matter where the array is defined, the elements of it should be initialized by the default constructor(The one without arguments); if there is no default constructor, then the elements must be explicitly initialized.

  3. Drawbacks of array:不能整体拷贝或复制,不能整体作为参数传入函数,要么退化为指针,要么退化为引用;不能作为函数的返回类型;不知道自己的大小,无法动态改变大小,插入更多值的方法只能是重建一个更大的数组再把值一个个复制过去。

  4. The right type to use for the index/size in array is size_t

Pointer

int a = 5;
int *pt = &a; // pointer to a
int **ppt = &pt; // pointer to the pointer pt
cout<<pt<<endl; // address of a, content of pt
cout<<ppt<<endl; // address of pt, content of ppt
cout<<*ppt<<endl; // address of a
cout<<**ppt<<endl; // 5

int *p0 = 0, *p1 = 0; // 0x00000000
int zero = 0;
int *p2 = zero;// wrong! Must known at compile time
void *p3 = &zero; // Any pointer to data can be implicitly
                 // converted to void*
cout << *(int*)p3; // 0

const int con = 4;
const int *p4 = &con; // pointer to const like const_iterator
int *p5 = &con; // error
int *const p6 = &zero; // const pointer, can't point to
                      // objects other than the one initialized, 
                     // actually lose the meaning of pointer
const int *const p7 = &con; // const pointer to const object




typedef string *pstring; // typedef is not simply repalcement,
                        // it has syntax meaning
// const pstring != const string*
// All of them are const pointers to string
// const pstring == pstring const == string *const


char ch1[] = {'K','T'};
cout << ch1;
//output: KT烫烫烫L?

char *ch2 = "ldfkj";
*ch2 = 'u'; // wrong!!! "ldfkj" is const char*, although it is 
           // allowed to initialize ch2 for historic reason, 
           // we should never change the content of it!!!


char *ch3 = ch1;
while(*ch3){
    //do somthing
    ++ch3;
}// Fail if the string is not null-terminated.
// What's worse, it will not crash but end output until
// it meets a null pointer somewhere
  1. A pointer holds the address of another object.

  2. &:address-of operator

  3. Uninitialized pointers are a common source of run-time errors. And we have no way to know if a pointer is valid.

  4. void* pointer is a special pointer type that can hold an address of any object. And in most cases, the pointer to a function can also be cast to void*. We can dereference it after it be cast to a specific pointer with real type

  5. When we use the name of an array in an expression, that name is automatically converted into pointer to the first elment of the array.

  6. When a pointer plus or subtract an integer, the value changed is related to the type of the pointer

  7. ptrdiff_t is a machine-specific signed integral type used to put the difference between pointers.

  8. We can use the subscript operator on any pointer, as long as that pointer points to an elment in an array. Also, the subscripts can be negative.

  9. It is legal to compute an address one past the end of an array or object.

  10. There is no way for the pointer to const to know if the object it points to is actually a const or not. So it treats all objects to which it might point as const.

    Think of pointers to const as “pointers that think they point to const”

  11. type *var; is plain pointer, type *const var; is const pointer.

  12. The type of a string literal is an array of const char, it can be implicitly transformed to const char*(actually char* is also allowed).

  13. The behavior of modifying the content of pointer which points to the address one past the end of an array is undefined.

  14. malloc/free vs new/delete; When we dynamically allocate an array, we specify the type and size but not a name of the array. Instead we return a pointer pointing to the head of the array. We can not use subscript in this case.

int *p_arr = new int[12];// built-in, not initialized
                         // class, default constructor

const int *p1 = new const int[10]; //error, uninitialized const array
const int *p2 = new const int[10](); //ok, value-initialized

int *p3 = new int[0]; // points to no element

delete [] p_arr; // 不可偏移



string str;
const char *p = str.c_str();



int arr[3] = {0, 1, 3};
vector<int> ivec(arr, arr+3);//Use array to init

//delete后可以赋值
int b = 5;
int *p1 = new int[3]();
int *p2 = &b;
delete [] p1;
p1 = p2;


// explicitly initialize only element 0 in each row
int ia[3][4] = { {1}, {}, {3} };

int (*pia)[4] = ia; // pointer to 2-D array
pia = &ia[1]; // refer to the second row

cout << pia << *pia; // means address of the second row 
                     // and address of first element of the second row, respectively
                     // same in value
cout << **pia; // 0

// parentheses are important
int *ip[4]; // array of pointers
int (*ip)[4]; // pointer to array



int temp[2][3] = { {1,2,3}, {4,5,6} };
int (*p)[3] = temp;
++p; // point to the second element of temp,
    // which is an array of 3 elements, so
   // *p is still still still a 【pointer】 pointing to the
  // first element of the second array of temp
cout << **p; // 4, not 2

typedef int int_arr[3];
int_arr *ip = temp;
for(int_arr *p = temp;p != temp + 2;++p)
    for(int *q = *p;q != (*p) + 3;++q)
        cout << *q << endl;
  1. 只要有new就要记得delete

  2. string 中没有返回char* 的函数,只有返回const char*的。

  3. char*在输出的时候,<<针对它重载过,直接输出其指向的字符串而非地址;若需要输出地址,先转为void*,或使用printf("%p",ptr)

Reference : C++ Primer 4th edition(评注版)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值