41 const const beijing university of posts

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: defghijklmnopqrstuvwxyz”; 另外:char str = “Jers\000Munk”; Beijing University of Posts & Telecommunications Beijing [email protected] 5.3 到数组的指针 数组名字可以被用作它的开始元素的指针 注意:取得“开始元素之前的一个位置”没有 任何意义,因为数组常常被分配在机器地址 的边界上 数组名可以被隐式地转换成数组的开始元素 的指针(这在C风格代码的函数调用中被广泛 使用),但是这将丢失数组的大小信息, vector和string没有此类问题 Beijing University of Posts & Telecommunications Beijing [email protected] int v = {1, 2, 3, 4}; int* p1 = v; int* p2 = &v[0]; int* p3 = &v[4]; //最后元素之后的一个位置 extern “C” int strlen(const char*); // string.h void f() { char v = “Annemarie”; char* p = v; //隐式地从char转换到char* strlen(p); strlen(v); //隐式地从char转换到char* v = p; //Error: 不能给数组赋值 } Beijing University of Posts & Telecommunications Beijing [email protected] 5.3.1 在数组中漫游 void fi(char v) { for(int i = 0; v[i] != 0; i++) use(v[i]); } 等价于 void fi(char v) { for(char* p =v; *p!=0; p++) use(v[i]); } 需要注意的是,普通数组不具备自描述性,当遍历 时,需要提供数组长度 Beijing University of Posts & Telecommunications Beijing [email protected] #include <iostream> int main() { int vi[10]; short vs[10]; std::cout << &vi[0] << ‘ ‘ << &vi[1] << endl; std::cout << &vs[0] << ‘ ‘ << &vs[1] << endl; } 可能输出 0x7fffaef0 0x7fffaef4 0x7fffaedc 0x7fffaede Beijing University of Posts & Telecommunications Beijing [email protected]
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern