C++编程(3)

41 const const beijing university of posts

Info iconThis 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

This note was uploaded on 02/10/2014 for the course ECE C++ taught by Professor Xiaoshengtang during the Fall '13 term at BUPT.

Ask a homework question - tutors are online