如何使用指针在 C 中的第一个 space 上拆分字符串?

How to split a string on the first space in C using pointers?

我正在尝试编写一个程序,允许用户输入他的全名,然后程序只输出他的全名。为此,我制作了一个指针,malloc 大小为 128 个字符,应该稍后存储名称。

我已经尝试 运行 使用 CLion、Netbeans、Code Blocks 的程序,现在 this site,但它们指出了一些错误...所以程序不能 运行.

问题是程序甚至没有编译,说有一些错误...这些是构建消息:

|=== Build: Debug in exerciseSixteen (compiler: GNU GCC Compiler) ===|
| LINE | MESSAGE 
|      |In function 'getSurname':
|  08  |warning: initialization makes integer from pointer without a cast [-Wint-conversion]
|      |In function 'main':
|  04  |error: expected ')' before numeric constant
|  17  |note: in expansion of macro 'MAX'
|  21  |warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]|
|=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

我什至卸载了 MinGW 编译器和 MSyS,并重新安装了同时安装 MinGW 的 CodeBlocks,但现在我知道我的问题与编译器或 IDE 无关...它是可能的代码,顺便说一下,可以在下面看到:

#include <stdio.h>
#include <stdlib.h>

#define MAX 128

char *getSurname(char *name)
{
    char space = " ";
    while (*name!=space) {
        name++;
    }
    return name;
}

int main()
{
    char *name = malloc(sizeof(char*MAX));
    printf("Insert your full name: ");
    gets(name);
    char surname = *getSurname(name);
    printf("Hello %s!\n", surname);
    return 0;
}

我看不到这段代码有任何错误,我认为一切正常...我错过了什么?我可以做些什么来让它发挥作用?

在 getSurname 函数中

char space = ' ';

" " 是字符串,不是字符。 需要变量类型 char*

在主函数中

char *name = (char*)malloc(sizeof(char) * MAX);

括号对不匹配

sizeof参数问题

char *surname = getSurname(name);

getSurname 函数不是指针函数。

getSurname 函数return类型是字符指针。

参数类型为getSurname函数的char* 参数 &name 是类型 char**

嗯,有几个错误:

  1. malloc(sizeof(char*MAX) 应该是 malloc(sizeof(char) * MAX).
  2. gets() 应该 永远不要使用 ,请使用 fgets(name, MAX, stdin).
  3. char space = " ";是错误的,应该是char space = ' ';,因为" "是字符串,不是字符。
  4. char surname = *getSurname(&name) 也有两个错误:首先,你不需要使用 *,你必须将 surname 声明为 char *,而不是 char;其次,您也不需要使用 &name 。正确的调用是 char *surname = getSurname(name);.
  5. 您还必须在扫描 name 时检查 '[=27=]',以避免超出字符串末尾。
  6. 你停在 while 中的 space,你应该再往前一个字符,在 returning 之前添加一个 name++ 以防 space 被发现。
  7. 您应该检查 malloc() 的 return 值,如果 returns NULL,请退出程序。
  8. 在从 main returning 之前 free(name) 是一个很好的做法,尽管这并不是严格需要的。

正确的代码是:

#include <stdio.h>
#include <stdlib.h>

#define MAX 128

char *getSurname(char *name)
{
    while (*name != '[=10=]' && *name != ' ')
        name++;

    if (*name == ' ')
        name++;

    return name;
}

int main(void)
{
    char *name = malloc(sizeof(char) * MAX);
    if (name == NULL) {
        perror("malloc() failed");
        return 1;
    }

    printf("Insert your full name: ");
    fgets(name, MAX, stdin);

    char *surname = getSurname(name);
    printf("Hello %s!\n", surname);

    free(name);
    return 0;
}

此外,作为旁注,fgets() 在第一个换行符处停止,因此您应该在处理之前从名称中删除换行符,否则您将得到如下输出:

Hello Relbeits
!

您可以在调用 fgets():

后执行此操作
char *nl = strchr(name, '\n');
if (nl != NULL)
    *nl = '[=12=]';

几个 错误。我在下面注释并修复了它们。我认为这样看会更容易。

#include <stdio.h>
#include <stdlib.h>
#if 1
#include <string.h>
#endif

#define MAX 128

char *
getSurname(char *name)
{
// NOTE/BUG: initialization of a _scalar_ must use single quotes
#if 0
    char space = " ";
#else
    char space = ' ';
#endif

// NOTE/BUG: this has no check for end of string
// NOTE/BUG: this returns a pointer to the _space_ and _not_ the first char
// of the last name
#if 0
    while (*name != space) {
        name++;
    }
#else
    for (;  *name != 0;  ++name) {
        if (*name == space) {
            ++name;
            break;
        }
    }
#endif

    return name;
}

int
main(void)
{
// NOTE/BUG: this is a syntax error [not enough right paren]
#if 0
    char *name = malloc(sizeof(char *MAX);
#else
    char *name = malloc(sizeof(*name) * MAX);
#endif

// NOTE/BUG: do _not_ use 'gets'
    printf("Insert your full name: ");
#if 0
    gets(name);
#else
    fgets(name,MAX,stdin);
    char *cp = strchr(name,'\n');
    if (cp != NULL)
        *cp = 0;
#endif

// NOTE/BUG: you want the address value of what name _points to_ and _not_
// the address of name itself
// NOTE/BUG: you want to return and save a _pointer_ and _not_ a scalar
#if 0
    char surname = *getSurname(&name);
#else
    char *surname = getSurname(name);
#endif
    printf("Hello %s!\n", surname);

    return 0;
}