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)。为什么指针指向堆栈或堆对函数很重要?我知道为什么您不能访问二进制/文本段中的字符串,但是堆栈有什么问题?
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并非旨在将指向数组的指针作为参数(这将阻止它根据需要重新分配)