字符串指针数组,也即该数组中的每一项都是一个指向字符串的指针。

定义:char* s[3];即包含三个指针的数组,写成这种形式也可以更好的理解,即数组存的类型就是char*。

另外一点:数组名一般是指首地址,所以对该数组的第一个元素取地址&s[0],由于s[0]是指针,所以数组名也就是一个指向指针的指针,char** p=s;

那么对该数组的操作如下:

int main()
{
    char* a="hello!";
    char* b="pangpang!";
    char* c="how are you?";
    char* s[3]={a,b,c};
    int len=sizeof(s)/sizeof(char*);
    char** p=s;
    for (int i=0;i<len;++i)
    {
        cout<<p[i]<<endl;
    }
    return 0;
}

  sizeof(s)的长度是该数组一共包含多少个字节,3个指针即12个字节(32位机),所以再除指针的长度4,既可以得出数组的长度为12/4=3

  或者这样输出也是可以的:

int main()
{
    char* a="hello!";
    char* b="pangpang!";
    char* c="how are you?";
    char* s[4]={a,b,c,NULL};
    char** p=s;
    for (p;*p!=NULL;++p)
    {
        cout<<*p<<endl;
    }
    return 0;
}

 

相关文章:

  • 2021-05-19
  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2021-04-22
  • 2021-10-27
  • 2021-09-01
  • 2022-12-23
猜你喜欢
  • 2021-07-12
  • 2022-12-23
  • 2021-05-23
  • 2021-08-22
  • 2022-12-23
  • 2021-12-22
  • 2021-05-30
相关资源
相似解决方案