C++字符数组存放字符串 | 字符指针变量

2022-08-02 14:37:14

C++指向数组的指针作函数参数

一维数组名可以作为函数参数传递,多维数组名也 可作函数参数传递。

C++用字符数组存放一个字符串

在C++中可以用多种方法访问一个字符串,第一种字符数组:

#include<iostream>//预处理usingnamespace std;//命名空间intmain()//主函数{char str[]="关注:C语言入门到精通";
  cout<<str<<endl;return0;//函数返回值为0;}

编译运行结果:

关注:C语言入门到精通--------------------------------
Process exited after3.446 seconds withreturn value0
请按任意键继续...

第二种,字符串变量,编译运行结果:

#include<iostream>//预处理#include<string>usingnamespace std;//命名空间intmain()//主函数{
  string str="关注:C语言入门到精通";
  cout<<str<<endl;return0;//函数返回值为0;}

编译运行结果:

关注:C语言入门到精通--------------------------------
Process exited after1.862 seconds withreturn value0
请按任意键继续...

第三种,字符指针变量,编译运行结果:

#include<iostream>//预处理#include<string>usingnamespace std;//命名空间intmain()//主函数{char*str="关注:C语言入门到精通";
  cout<<str<<endl;return0;//函数返回值为0;}

编译运行结果:

关注:C语言入门到精通--------------------------------
Process exited after1.483 seconds withreturn value0
请按任意键继续...

对字符串中字符的存取,可以用下标方法,也可以用指针方法。

C++字符指针变量


  • 作者:小林C语言
  • 原文链接:https://blog.csdn.net/weixin_48669767/article/details/111352405
    更新时间:2022-08-02 14:37:14