删除字符串 C 的第一个字符

发布于 2024-09-12 01:16:51 字数 437 浏览 5 评论 0原文

我试图删除字符串的第一个字符并保留其余部分,我当前的代码无法编译,我对如何修复它感到困惑。

我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

很酷又爱笑 2024-09-19 01:16:51

字符串在 C 中不是这样工作的。您将缓冲区中的所有字符汇总到 v 变量中。您不能使用 + 来连接。您发布的代码存在一些严重的问题,这表明在如何使用 C 方面存在理解差距。

尝试这个:

char *newStr (char *charBuffer) {
  int length = strlen(charBuffer);
  char *str;
  if (length <= 1) {
    str = (char *) malloc(1);
    str[0] = '\0';
  } else {
    str = (char *) malloc(length);
    strcpy(str, &charBuffer[1]);
  }
  return str;
}

或这个:

char *newStr (char *charBuffer) {
  char *str;
  if (strlen(charBuffer) == 0)
    str = charBuffer;
  else
    str = charBuffer + 1;
  return str;
}

取决于您是否要分配新字符串。您还必须添加代码来处理不以“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:

char *newStr (char *charBuffer) {
  int length = strlen(charBuffer);
  char *str;
  if (length <= 1) {
    str = (char *) malloc(1);
    str[0] = '\0';
  } else {
    str = (char *) malloc(length);
    strcpy(str, &charBuffer[1]);
  }
  return str;
}

or this:

char *newStr (char *charBuffer) {
  char *str;
  if (strlen(charBuffer) == 0)
    str = charBuffer;
  else
    str = charBuffer + 1;
  return str;
}

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.

不美如何 2024-09-19 01:16:51

好吧,你的描述说你想处理“字符串”,但你的代码处理字符缓冲区/指针。删除字符串第一个字符的最简单方法是,

const char *newStr(const char *string)
{
    return string+1;
}

但由于这看起来完全不像您的代码所做的,因此您可能想要不同的东西。例如,如果您只想删除前导的“A”或“Q”,然后将字符串复制到缓冲区,您需要类似的内容

char *newStr(const char *string)
{
    if (string[0] == 'A' || string[0] == 'Q')
        string++;
    return strdup(string);
}

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

const char *newStr(const char *string)
{
    return string+1;
}

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 *newStr(const char *string)
{
    if (string[0] == 'A' || string[0] == 'Q')
        string++;
    return strdup(string);
}
長街聽風 2024-09-19 01:16:51

您只需将 char 指针移动一个字符即可:

char* newstring = oldstring + 1;

You can simply move your char pointer one character in:

char* newstring = oldstring + 1;
满天都是小星星 2024-09-19 01:16:51

您的函数被声明返回一个 char * 并且您正在返回一个 char。

此外,为什么不返回指向第二个字符的指针呢?

char * newStr (char * charBuffer)
{
   if (charBuffer && (*charBuffer == 'A' || *charBuffer == 'Q')) return charBuffer + 1;
   return charBuffer;
}

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?

char * newStr (char * charBuffer)
{
   if (charBuffer && (*charBuffer == 'A' || *charBuffer == 'Q')) return charBuffer + 1;
   return charBuffer;
}
南风起 2024-09-19 01:16:51

其他几个答案建议返回 charBuffer + 1。正如我在之前的评论中指出的:

这是不好的做法。如果字符串是动态分配的怎么办?也许最终存储将被释放(从第二个字符开始)。应首先将字符串复制到新存储。

从中间释放一块存储将导致未定义的行为。

相反,请尝试 strdup 函数将返回给定字符串的重复项。

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

char *newStr(char* charBuffer) {
    if (charBuffer && (charBuffer[0] == 'A' || charBuffer[0] == 'Q'))
        return strdup(charBuffer + 1);
    else 
        return strdup(charBuffer);
}

void main() {
    char a[7] = "Advait";
    char b[5] = "John";
    printf("%s\n",newStr(a));   // Prints "dvait"
    printf("%s\n",newStr(b));   // Prints "John"
}

Several of the other answers recommended returning charBuffer + 1. As I noted in my previous comment:

This is bad practice. What if the string is dynamically allocated? Perhaps eventually the storage will be freed (starting from the second character). The string should be copied to new storage first.

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.

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

char *newStr(char* charBuffer) {
    if (charBuffer && (charBuffer[0] == 'A' || charBuffer[0] == 'Q'))
        return strdup(charBuffer + 1);
    else 
        return strdup(charBuffer);
}

void main() {
    char a[7] = "Advait";
    char b[5] = "John";
    printf("%s\n",newStr(a));   // Prints "dvait"
    printf("%s\n",newStr(b));   // Prints "John"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文