【发布时间】:2021-12-28 17:44:53
【问题描述】:
我已经在 linux 中使用 PCRE 在 C 中创建了一个脚本,用于匹配字符串中的一个单词,它可以工作,但现在我想修改它,因为我希望它匹配,短语中的所有相同单词,我这样做这个模组。但返回给我警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“pcre_get_substring”的参数 2 ,对不起,我是 C 的初学者程序员,谁能给我一个解释的解决方案?谢谢
* gcc -Wall pcre1.c -I/usr/local/include -L/usr/local/lib -R/usr/local/lib -lpcre
* or
* gcc -Wall pcre1.c -I/usr/local/include -L/usr/local/lib -lpcre
* or
* gcc pcre1.c -lpcre
*/
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#define OVECCOUNT 30 /* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
int main()
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
int offsetcount;
int offsets[(0+1)*3]; // (max_capturing_groups+1)*3
char *result;
char src[] = "111 <title>Hello World</title> <title>Hello World</title>222";
char pattern[] = "<title>(.*)</title>";
re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s/n", erroffset, error);
return 1;
}
offsetcount = pcre_exec(re, NULL, src, strlen(src), 0, 0, offsets, (0+1)*3);
while (offsetcount > 0) {
if (pcre_get_substring(src, &offsets, offsetcount, 0, &result) >= 0) {
// Do something with match we just stored into result
printf("de %s/n",result);
}
offsetcount = pcre_exec(re, NULL, src, strlen(src), 0, offsets[1], offsets, (0+1)*3);
}
free(re);
return 0;
}```
【问题讨论】:
-
&offsets应该只是offsets,因为offsets已经是一个指针/数组。 -
我删除 &offsets 到 -> 偏移和 &result -> 结果,因为给我同样的错误,但现在告诉我警告:从不兼容的指针类型传递 'pcre_get_substring' 的参数 5 [-Wincompatible-pointer-types ] 63 | if (pcre_get_substring(src, offsets, offsetcount, 0, result) >= 0) { | ^~~~~~ | | |字符 *
-
(const char **) &result -
谢谢它的工作,我也有一些问题,再次抱歉,1.(const char **) &result 是指针的演员?我明白好吗? 2. 如果我想要打印结果,我插入``` if (pcre_get_substring(src, offsets, offsetcount, 0, (const char **) &result) >= 0) { printf("de %s/n", &result); }``` 但返回错误“%s”需要“char *”类型的参数,但参数 2 的类型为“const char **”
-
是的,这是一个演员表。
pcre_get_substring的第 5 个参数的类型必须是const char **。只需将result与printf一起使用,而不是&result。如果您想查看打印的内容,请将模式更改为"<title>.*</title>",因为您的代码不会按原样打印捕获组内容。