473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

returning pointer into const argument

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
Nov 14 '05 #1
3 1310
System Administrator wrote:
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.
Nov 14 '05 #2
"System Administrator" <ro**@cogsci.ed.ac.uk> wrote in message
news:c6***********@pc-news.cogsci.ed.ac.uk...

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

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #3

"Stephen Sprunk" <st*****@sprunk.org> wrote in message
news:06******************************@news.teranew s.com...
"System Administrator" <ro**@cogsci.ed.ac.uk> wrote in message
news:c6***********@pc-news.cogsci.ed.ac.uk...

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

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin


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

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
3
by: Paul Kirby | last post by:
Hello All I am trying to create a function that returns a string like the following: // defined in headerfile // void test_func(char *retstr); // -------------------- // // c/cpp File //
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
36
by: MC felon | last post by:
how do we return strings or arrays from a function? i tried.. char some_func() //where i thought char would tell the compiler that the return is a string { char str; //something; return...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.