C++编程(3)

C 编程(3)

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: ; // ok inv v5[8] = {1, 2, 3, 4}; 相当于int v5[8] = { 1, 2, 3, 4, 0, 0, 0, 0}; 注意:不存在与数组初始化相对应的数组赋值 void f() { v4 = { ‘c’, ‘d’, 0}; // Error } 可以使用vector或者valarray进行赋值 Beijing University of Posts & Telecommunications Beijing [email protected] 5.2.2 字符串文字量 字符串文字量是用双引号括起来的字符序列 一个字符串文字量里包含的字符个数比它看起来的 字符数多一个,它总是由一个空字符“\0”结束,空 字符的值是0 例如:sizeof(“test”)=5,但是strlen(“test”)=4 字符串文字量的类型是“适当个数的const字符的数 组”,所以“test”的类型就是const char[5],此为 常量,而且是静态分配的 可以用字符串文字量给一个char*赋值,若想修改 一个字符串,可以先将其复制到一个数组中去 Beijing University of Posts & Telecommunications Beijing [email protected] 字符串示例 void f() { char* p = “Plato”; p[3] = ‘e’; //Error: 给常量赋值, 结果无定义 } void f2() { char p = “Zeno”; p[0] = ‘R’; // ok } const char* error_msg() { // … return “range error”; } const char* p = “Heraclitus”; const char* q = “Heraclitus”; void g() { if(p==q) cout << “One!”; //… } Beijing University of Posts & Telecommunications Beijing [email protected] 字符串示例 字符串中不能有真正的换行,比如 char str = “ this is a two lines string”; //Error char str = “this is a two \n lines string”; 可以:char alpha = “abcdefghijklmn” “opqrstuvwxyz”; 等价于 char alpha = “abc...
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