014-C++中字符数组和字符常量

#include <iostream>

using namespace std;
/**
 * 字符串变量
 * 
 * 字符串常量 
 * 
 * 数组变量的值是不允许改变的
 * 
 * 字符串的指针表示方法
 * 指针表示方法:
 * char* pStrHelloWorld = "helloworld";
 * 
 * char[] 和 char*的区别: 
 * 地址和地址存储的信息
 * 可变与不可变
 * 
 * 如:
 * char strHelloWorld[11] = {"helloworld"};
 * strHelloworld不可变(就是将strHelloworld指向其他变量)
 * strHelloworld[index]的值可变
 * 
 * char* pStrHelloWorld = "helloworld";
 * pStrHelloworld可变,但是pStrHelloWorld[index]的值可变不可变取决于
 * 所指向存储区是否可变。 
*/
int main(int argc, char const *argv[])
{
  /* code */
  char strHelloWorld[12] = {"helloworld"};
  char strhelloWordl2[]  = {"helloworld"};
  char c1 = 0;
  char c2 = '\0';
  char c3 = '0'; 
  
  cout<<strHelloWorld<<endl;
  cout<<strhelloWordl2<<endl;
  // 字符串的指针表示方法 "hellworld"是字符串常量 
  // strHelloWorld 是字符串变量
  char* pStrHelloWorld = "helloworld";
  cout<<pStrHelloWorld<<endl;


  // 字符数组和字符串指针
  for (int i = 0; i < 10; i++)
  {
    /* code */
    strHelloWorld[i]+=1;
    cout<<strHelloWorld[i]<<endl;
  }
  for (int i = 0; i < 10; i++)
  {
    /* code */
    // pStrHelloWorld[i]+=1;
    // cout<<pStrHelloWorld[i]<<endl;
    cout<<*(pStrHelloWorld+i)<<endl;
  }
  return 0;
}

输出如下:

helloworld
helloworld
helloworld
i
f
m
m
p
x
p
s
m
e
h
e
l
l
o
w
o
r
l
d

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值