英文:
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*
然后我在线查看了这个程序,我看到的index
和rindex
函数的定义都是相同的:
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
我想知道为什么我不能这样做,以及正确使用index
和rindex
函数的方式,或者正确的函数定义,请告诉我。
我的环境如下:
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 <stdio.h>
#include <string.h>
int main()
{
const char* str = "abcdefgabcdefg";
char* index_first = index(str,'c');
printf("the first index is %ld\n",index_first - str + 1);
char* index_last = rindex(str,'c');
printf("the last index is %ld\n",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,'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);
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论