I'm working with PCRE library for C on a linux x86_64 system, though I don't think the PCRE is to blame for the issue I'm having. Basically I have an array of character arrays that holds the result from the PCRE check. I used typedef to keep it clean
typedef char *pcreres[30];
And the function that handles checking for matches, etc
int getmatch(const char *pattern, char *source, pcreres *res){
const char *error;
int erroffset,开发者_StackOverflow中文版 rc,i;
int ovector[30];
pcre *re = pcre_compile(pattern,PCRE_CASELESS | PCRE_MULTILINE, &error,&erroffset,NULL);
rc=pcre_exec(re,NULL,source,(int)strlen(source),0,0,ovector,30);
if(rc<0){
return -1;
}
if(rc==0) rc=10;
for(i=0;i<rc;i++){
char *substring_start=source+ovector[2*i];
int substring_length=ovector[2*i+1] - ovector[2*i];
*res[i] = strndup(substring_start,substring_length);
}
return rc;
}
The code I'm testing has 2 results and if I put a printf("%s",*res[1]) in the function just before the return I get the expected result.
However in my main function where I call getmatch() from I have this code;
pcreres to;
mres=getmatch(PATTERN_TO,email,&to);
printf("%s",to[1]);
I get an empty string, however to[0] outputs the correct result.
I'm somewhat a newbie at C coding, but I'm completely lost at where to go from here.
Any help is appreciated!
Operator precedence. the []
operator is evaluated before the *
operator. In your function try this:
(*res)[i] = strndup(substring_start,substring_length);
精彩评论