Mail Archives: djgpp/2002/04/11/14:31:23
> From: "Goh, Yong Kwang" <gohyongkwang AT hotmail DOT com>
> Newsgroups: comp.os.msdos.djgpp
> Date: Thu, 11 Apr 2002 16:51:10 +0800
>
> int regexec(const regex_t *preg, const char *string, size_t nmatch,
> regmatch_t pmatch[], int eflags);
>
> So prior to calling regexec(), do we have to allocate memory for the
> pmatch[] array of regmatch_t structures or it'll be dynamically allocated by
> regexec() itself as it finds sub-expressions matches, conforming to the
> compiled regex, in the string?
You have to allocate it.
> If we have to allocate memory for pmatch[], how do we know how many
> sub-expressions there are in the regex, assuming the regex is only known
> when the program is run
The number of subexpressions is returned by regcomp in the
preg->re_nsub member (see the docs of regcomp, which explains this).
> Also, one thing that I cannot understand is that that size_t nmatch is
> supposed to hold the number of matches (sub-expressions) it could find in
> the string.
This argument is input to regexec, not output from it. So it's
passed by value and is not changed by regexec.
> In another word, the size of pmatch[] is already fixed and memory
> has already been allocated for pmatch[] prior to calling regexec.
That's correct.
- Raw text -