删除字符串 C 的第一个字符
我试图删除字符串的第一个字符并保留其余部分,我当前的代码无法编译,我对如何修复它感到困惑。
我的代码:
char * newStr (char * charBuffer)
{
int len = strlen(charBuffer);
int i = 1;
char v;
if(charBuffer[0] == 'A' || charBuffer[0] == 'Q'){
for(i=1;i<len;i++)
v = v + charBuffer[i];
}
v = v + '\0';
return v;
}
Gcc:“警告:返回使指针来自整数而不进行强制转换”
另外:“char * newStr(char * charBuffer)”需要保持不变。
Im trying to remove the first char of the string and keep the remainder, my current code doesnt compile and im confused on how to fix it.
My code:
char * newStr (char * charBuffer)
{
int len = strlen(charBuffer);
int i = 1;
char v;
if(charBuffer[0] == 'A' || charBuffer[0] == 'Q'){
for(i=1;i<len;i++)
v = v + charBuffer[i];
}
v = v + '\0';
return v;
}
Gcc: "Warning: return makes pointer from integer without a cast"
Also: "char * newStr (char * charBuffer)" needs to remain the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
字符串在 C 中不是这样工作的。您将缓冲区中的所有字符汇总到
v
变量中。您不能使用 + 来连接。您发布的代码存在一些严重的问题,这表明在如何使用 C 方面存在理解差距。尝试这个:
或这个:
取决于您是否要分配新字符串。您还必须添加代码来处理不以“Q”或“A”开头的情况。我没有包括这些,因为我不确定你到底想在这里做什么。
确保您对使用 malloc 和 free 分配和释放内存进行了一些研究。如果您要进行 C 编程,这些是可以使用的基本函数。
Strings don't work like this in C. You're summing up all of the characters in the buffer into the
v
variable. You can't use + to concatenate. The code you posted has some serious problems which indicate that there's an understanding gap with how to use C.Try this:
or this:
Depending on whether you want to allocate a new string or not. You'll also have to add the code for handling the cases that don't start with 'Q' or 'A'. I didn't include those because I'm not sure exactly what you're trying to do here.
Make sure you do some research into allocating and deallocating memory with malloc and free. These are fundamental functions to be able to use if you're going to be doing C programming.
好吧,你的描述说你想处理“字符串”,但你的代码处理字符缓冲区/指针。删除字符串第一个字符的最简单方法是,
但由于这看起来完全不像您的代码所做的,因此您可能想要不同的东西。例如,如果您只想删除前导的“A”或“Q”,然后将字符串复制到缓冲区,您需要类似的内容
Well, your description says you want to deal with "strings", but you code deals with char buffers/pointers. The simplest approach to remove the first character for strings would be
but as that doesn't look at all like what your code is doing, you probabaly want something different. For example, if you want to just remove a leading 'A' or 'Q' and then copy the string to a buffer, you want something like
您只需将 char 指针移动一个字符即可:
You can simply move your char pointer one character in:
您的函数被声明返回一个 char * 并且您正在返回一个 char。
此外,为什么不返回指向第二个字符的指针呢?
Your function is declared to return a char * and you are returning a char.
Furthermore, why don't you just return a pointer to the second character?
其他几个答案建议返回 charBuffer + 1。正如我在之前的评论中指出的:
从中间释放一块存储将导致未定义的行为。
相反,请尝试
strdup
函数将返回给定字符串的重复项。Several of the other answers recommended returning
charBuffer + 1
. As I noted in my previous comment:Freeing a piece of storage from the middle will result in undefined behavior.
Instead, try the
strdup
function which will return a duplicate of the given string.