2

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, 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!

1 Answer 1

3

Operator precedence. the [] operator is evaluated before the * operator. In your function try this:

(*res)[i] = strndup(substring_start,substring_length);
Sign up to request clarification or add additional context in comments.

1 Comment

That was it! I'm sure I won't forget this in the future. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.