为什么strsep()不使用指向堆栈的指针?

The*_*man 2 c string heap stack strsep

似乎使用该函数strsep查找字符串的第一个字存在指针兼容性问题。到目前为止,我一直认为char *s并且char s[]可以完全互换。但是看来它们不是。我的程序在堆栈上使用数组失败,并显示以下消息:

foo.c: In function ‘main’:
foo.c:9:21: warning: passing argument 1 of ‘strsep’ from incompatible pointer type [-Wincompatible-pointer-types]
  char *sub = strsep(&s2, " ");
                     ^
In file included from foo.c:2:0:
/usr/include/string.h:552:14: note: expected ‘char ** restrict’ but argument is of type ‘char (*)[200]’
 extern char *strsep (char **__restrict __stringp,
Run Code Online (Sandbox Code Playgroud)

我不明白这个问题。该程序使用的malloc作品。

这有效:

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

int main(void)
{
    char s1[] = "Hello world\0";
    char *s2 = malloc(strlen(s1)+1);
    strcpy(s2, s1);
    char *sub = strsep(&s2, " ");

    printf("%s\n", sub);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这不是:

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

int main(void)
{
    char s1[] = "Hello world\0";
    char s2[200];
    strcpy(s2, s1);
    char *sub = strsep(&s2, " ");

    printf("%s\n", sub);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

怎么了 (对不起strcpy)。为什么指针指向堆栈或堆对函数很重要?我知道为什么您不能访问二进制/文本段中的字符串,但是堆栈有什么问题?

Dav*_*ica 6

 note: expected ‘char ** restrict’ but argument is of type ‘char (*)[200]’
Run Code Online (Sandbox Code Playgroud)

您的警告将准确告诉您问题所在。您有两种不同的类型。

char *s2;        /* declares a character pointer */
Run Code Online (Sandbox Code Playgroud)

char s2[200];   /* declares an array of char[200] */
Run Code Online (Sandbox Code Playgroud)

当您获取一个指针的地址时,结果是一个指向指针的指针。当您获取数组的地址时,结果是指向数组指针。当您取消引用指针到指针时,结果是一个指针。当取消引用数组指针时,结果是array

strsep并非旨在将指向数组指针作为参数(这将阻止它根据需要重新分配)

  • 了解[C11标准-6.3.2.1其他操作数-左值,数组和函数指示符(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3)解释了如何数组在访问时转换为指针。尽管在某些情况下可以自动使用具有自动存储期限的阵列和动态分配的阵列,但它不会更改基础类型。问题是当函数需要一个指向指针的指针时,例如`char * strsep(char ** stringp,const char * delim);`或`ssize_t getline(char ** lineptr,size_t * n,FILE *流);` (2认同)