warning: deprecated conversion from string constant to ‘char*’

来源:互联网 发布:程序员竖屏显示器 编辑:程序博客网 时间:2024/05/18 01:39

 

My program is :

 


#include <iostream>

using namespace std;

  int main()

  {
   int i;
   char   *pArray[]={"How ","are ","you"};
    char **p;
   p=pArray;
   cout << endl;
   for (i=0; i<3; i++)
   cout << "*{p+1}"<< *(p+i);
  }

 

but when I Build this program,have some warning:    : warning: deprecated conversion from string constant to ‘char*’

 

Why it is give me " : warning: deprecated conversion from string constant to ‘char*’ "

 

How can I  fix thist?

 

answer:

A string literal is a constant value in modern C and C++, so the pointer to it has const char* type (not char* )

 

附:

指向指针的指针

指向指针的指针也称为二级指针,它指向的是指针型的数据。定义指向指针的指针变量的格式为:
数据类型 **指针变量名称;
下面通过一个实例说明指向指针的指针变量的用法。
main()
{
int i;
char *pArray[]={"How","are","you"};
char **p;
p= pArray;
printf("/n");
for(i=0;i<3;i++)
printf("%s ", *(p+i));
}
程序运行的结果为:
How are you
变量的内存情况如图(内存单元首地址为假设地址值)。说明:
(1)语句char **p;定义了一个指向指针数据的指针变量,该指针数据为一个指向char型变量或char型数组的指针。
(2)指向指针数据的指针变量也是一个变量,有自己的内存空间。它保存的也是一个指针值(指针的指针),是一个unsigned int型数,在TurboC下占用2个字节的内存。
(3)语句p= pArray;将指针数组的首地址FFC0(即第一个元素的指针)赋给p,p指向了指针数组的首地址。
(4)*p的值为指针值FF10,即为pArray[0]的值。
(5)p+1的值为指针值FFC2。因为*p为指针型,即unsigned int型,占用两个字节,根据指针的加法规则,p+1的值为FFC2。

上面的例子中p没有发生变化。p是一个变量,其值是可以变化的。下面的代码中,每次p指向下一个字符数组的首地址,利用了p的自加打印字符串的值:
main()
{
int i;
char *pArray[]={"How","are","you"};
char **p;
p= pArray;
printf("/n");
for(i=0;i<3;i++,p++)
printf("%s ", *p);
}
例 8-11 输入若干字符串,统计其中"China"字符串出现的次数(使用指向指针的指针)。
程序可以使用malloc函数动态定义数组:首先动态定义指针数组,并让指向指针的指针pArray指向该数组。然后在循环过程动态定义字符数组,并将每个字符数组的指针保存在前面定义的指针数组中。
统计"China"字符串次数的方法很简单,只要利用循环,用strcmp比较并累加即可。
统计过程结束后,先释放每个字符数组,再释放指针数组。注意这个过程不能颠倒,否则会丢失字符数组指针的信息。
char **DefinePointerArray (int n);
char *DefineCharArray (int n);
void FreePointerArray (char **p);
void FreeCharArray (char *p);
main()
{
char **pArray, t[100];
int i, nCount, nLen, nChinaCount=0;

/*输入字符串个数*/
printf("/nPlease input number of the strings: ");
scanf("%d",&nCount);

/*定义动态指针数组,返回数组的指针*/
pArray= DefinePointerArray (nCount);

/*输入多个字符串*/
printf("Please input strings: /n");
for(i=0;i<nCount;i++)
{
scanf("%s",t);
nLen=strlen(t);

/*定义长度为nLen+1的动态char型数组,
数组首地址存入指针数组pArray [i]。
nLen+1是因为包括了字符串结束符*/
pArray [i]=DefineCharArray (nLen+1);/*pArray [i]等价于*(pArray+i)*/

/*将临时字符数组t里的内容拷贝到pArray[i]对应的字符数组*/
strcpy(pArray [i],t);
}

/*统计"China"字符串的个数*/
for(i=0;i<nCount;i++)
{
if(strcmp("China", *(pArray+i))==0)
nChinaCount++;
}

printf("There are %d 'China'.", nChinaCount);

/*先释放指针数组pArray[i]对应的字符数组*/
for(i=0;i<nCount;i++)
FreeCharArray (pArray[i]);

/*释放指针数组pArray*/
FreePointerArray (pArray);
}

/*定义动态的指针数组,返回指针数组的首地址*/
char **DefinePointerArray (int n)
{
return (char **) malloc(n*sizeof(char*));
}

/*定义动态的字符数组,返回字符数组的首地址*/
char *DefineCharArray (int n)
{
return (char *) malloc(n*sizeof(char));
}

/*释放动态指针数组*/
void FreePointerArray (char **p)
{
free((void*)p);
}

/*释放动态字符数组*/
void FreeCharArray (char *p)
{
free((void*)p);
}

 

 

 

原创粉丝点击