gpt4 book ai didi

c++ - 使用指向字符串的指针初始化指向字符串中字符的指针

转载 作者:行者123 更新时间:2023-11-30 04:19:20 25 4
gpt4 key购买 nike

int isPurePalindrome(const char *sentence){ 
int i;
if (sentence="")
return 0;
char *begin; char *end; i=strlen(sentence);//i is now the length of sentence string
*begin= &sentence(0);
*end=&sentence[i-1];

我正在使用开发 C++。我试图初始化指针“开始”以指向字符串“句子”中的第一个字符,但它一直给我一个错误“赋值从指针生成整数而不进行强制转换”。“*sentence”指向用户在main中输入的字符串。 ispalindrome 是我正在编写的函数。如果在 main 中输入的句子是 NULL,它应该返回 0。

最佳答案

您的代码存在一些问题:

if (sentence="") return 0; 

应该是

if (strcmp(sentence,"")==0) return 0;

char *begin; char *end;

应该是

const char *begin; const char *end;

*begin= &sentence(0); 

应该是

begin = &sentence[0];

*end=&sentence[i-1];

应该是

end = &sentence[i-1];

关于c++ - 使用指向字符串的指针初始化指向字符串中字符的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15893522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com