Invalid conversion from ‘const char*’ to ‘char*’ with `rindex` function.

huangapple go评论70阅读模式
英文:

Invalid conversion from ‘const char*’ to ‘char*’ with ```rindex``` function

问题

我想在C++17下使用```index```和```rindex```函数处理字符串,但当我编译程序时,出现了以下错误:

```plaintext
debug.cpp: In function int main():
debug.cpp:7:27: error: invalid conversion from const char* to char* [-fpermissive]
    7 |  char* index_first = index(str, 'c');
      |                      ~~~~~^~~~~~~~~
      |                           |
      |                           const char*
debug.cpp:9:27: error: invalid conversion from const char* to char* [-fpermissive]
    9 |  char* index_last = rindex(str, 'c');
      |                     ~~~~~~^~~~~~~~~
      |                           |
      |                           const char*

然后我在线查看了这个程序,我看到的indexrindex函数的定义都是相同的:

char* index(const char* s, int c);
char* rindex(const char* s, int c);

这是我的调试代码:

#include <stdio.h>
#include <string.h>

int main()
{
	const char* str = "abcdefgabcdefg";
	char* index_first = index(str, 'c');
	printf("第一个索引是 %ld\n", index_first - str + 1);
	char* index_last = rindex(str, 'c');
	printf("最后一个索引是 %ld\n", index_last - str + 1);
	return 0;
}

我使用以下命令进行编译:

g++ -o debug debug.cpp -std=c++17

我想知道为什么我不能这样做,以及正确使用indexrindex函数的方式,或者正确的函数定义,请告诉我。

我的环境如下:

Ubuntu LTS 20.04 (x64)
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

感谢您的帮助。


<details>
<summary>英文:</summary>

I want to do something with string using the ```index``` and ```rindex``` function under c++17, but when I compile the program, this error poped up:

debug.cpp: In function ‘int main()’:
debug.cpp:7:27: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
7 | char* index_first = index(str,'c');
| ^~~~
| |
| const char*
debug.cpp:9:27: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
9 | char* index_last = rindex(str,'c');
| ^~~
| |
| const char*

Then I checked this program online, every function defines of ```index``` and ```rindex``` I saw are the same:

char* index(const char* s,int c);
char* rindex(const char* s,int c);

And heres my debug code:
```c++
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main()
{
	const char* str = &quot;abcdefgabcdefg&quot;;
	char* index_first = index(str,&#39;c&#39;);
	printf(&quot;the first index is %ld\n&quot;,index_first - str + 1);
	char* index_last = rindex(str,&#39;c&#39;);
	printf(&quot;the last index is %ld\n&quot;,index_last - str + 1);
	return 0;
 }

I compile it using:

g++ -o debug debug.cpp -std=c++17

I want to know why can't I do that and the right way to use index and rindex functions and (or) the right function defines please.

Heres my environment:

Ubuntu LTS 20.04 (x64)
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

Thank you for all the help.

答案1

得分: 1

你试图将在函数内部使用的返回类型为 const char * 的指针分配给类型为 char * 的指针。

实际上,你调用的函数声明如下:

const char* index(const char* s, int c);
const char* rindex(const char* s, int c);

在C++中,这些函数可以重载为:

const char* index(const char* s, int c);
const char* rindex(const char* s, int c);

char* index(char* s, int c);
char* rindex(char* s, int c);

与一些其他标准C函数,例如标准C函数 strchr 一样。因此,你应该这样写:

const char* index_first = index(str, 'c');
printf("the first index is %td\n", index_first - str + 1);
const char* index_last = rindex(str, 'c');
printf("the last index is %td\n", index_last - str + 1);

两个指针相减的结果具有有符号整数类型 ptrdiff_t。因此,你需要使用转换说明符 %td 而不是 %ld

根据C标准(7.21.6.1 The fprintf function):

7 长度修饰符及其含义为:

t 指定接下来的d、i、o、u、x或X转换说明符适用于ptrdiff_t或相应的无符号整数类型参数;或者指定接下来的n转换说明符适用于指向ptrdiff_t参数的指针。

英文:

You are trying to assign returned pointers of the type const char * that are used within the functions to pointers of the type char *

Actually the functions you are calling are declared like

const char* index(const char* s,int c);
const char* rindex(const char* s,int c);

In C++ the functions can be overloaded like

const char* index(const char* s,int c);
const char* rindex(const char* s,int c);

and

char* index(char* s,int c);
char* rindex(char* s,int c);

the same way as some other standard C functions as for example the standard C function strchr.

So you should write

const char* index_first = index(str,&#39;c&#39;);
printf(&quot;the first index is %td\n&quot;,index_first - str + 1);
const char* index_last = rindex(str,&#39;c&#39;);
printf(&quot;the last index is %td\n&quot;,index_last - str + 1);

The result of subtracting two pointers has the signed integer type ptrdiff_t. So you need to use the conversion specifier %td instead of %ld.

From the C Standard (7.21.6.1 The fprintf function)

> 7 The length modifiers and their meanings are:
>
> t Specifies that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer
> type argument; or that a following n conversion specifier applies to a
> pointer to a ptrdiff_t argument.

huangapple
  • 本文由 发表于 2023年2月18日 00:19:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定