returning pointer into const argument

  • Thread starter System Administrator
  • Start date
S

System Administrator

I have a function

int match(struct regexp *regexp, char *string, char **tail)

which matches a string against a regular expression, and if it
succeeds sets *tail to be the unmatched tail of the string.

I'd like the string argument to be const char *, to document the fact
that the match function doesn't modify it, but then I can't assign the
tail without an explicit de-constifying cast.

Should I give up the idea of using const here?

-- Richard
 
J

Jeremy Yallop

System said:
I have a function

int match(struct regexp *regexp, char *string, char **tail)

which matches a string against a regular expression, and if it
succeeds sets *tail to be the unmatched tail of the string.

I'd like the string argument to be const char *, to document the fact
that the match function doesn't modify it, but then I can't assign the
tail without an explicit de-constifying cast.

Adding the const (and the cast) seems reasonable here. That's how
the strtol() family handle the same issue.

Jeremy.
 
S

Stephen Sprunk

Aside: doing any user-type tasks (e.g. posting to usenet) as root is poor
form -- you should only be root when necessary for a specific task.
I have a function

int match(struct regexp *regexp, char *string, char **tail)

which matches a string against a regular expression, and if it
succeeds sets *tail to be the unmatched tail of the string.

I'd like the string argument to be const char *, to document the fact
that the match function doesn't modify it, but then I can't assign the
tail without an explicit de-constifying cast.

Should I give up the idea of using const here?

Does "char * const * tail" remove the need for a cast?

S
 
X

Xenos

Stephen Sprunk said:
Aside: doing any user-type tasks (e.g. posting to usenet) as root is poor
form -- you should only be root when necessary for a specific task.


Does "char * const * tail" remove the need for a cast?

S

don't you mean: const char** tail (or even: char const ** tail)? Its the
char that is constant, not the pointer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top