Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8874

Re: pointer to a pointer problems

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: pointer to a pointer problems
Date 2011-08-15 15:40 -0400
Organization A noiseless patient Spider
Message-ID <j2bsmi$h54$1@dont-email.me> (permalink)
References <ba16570a-e19d-48ad-804f-19cf73bc190d@s2g2000vby.googlegroups.com>

Show all headers | View raw


On 8/15/2011 2:16 PM, Christopher wrote:
> An open book test I am taking, tasked me to write a function with the
> following prototype. Yea, its a test, so don't give me the
> implementation. But it is open book, so maybe I can at least get a few
> questions answered.
>
> // returns the next token in a string found seperated by the
> delimeter. upon return updates str to point to the next token in
> // the string after the one returned
> const char * nextToken(const char ** str, const char delimeter);
>
> When I look at this, it looks very c-style to me. I think it is
> unsafe.

In what sense is it unsafe?  You're writing that function.  You can 
impose any requirements on the data the function receives and introduce 
all checks necessary...

> 1) I don't see a way to return a different string without allocating
> it in the function body and leaving it to the caller to clean the new
> string up.

What you get is a pointer to char [by means of its address], right? 
Supposedly it points to a sequence of characters.  Each character in 
that sequence has an address, yes?  One of the characters actually 
starts the "token" you need to find.  The address of that character you 
will need to return.

> 2) Why we have a pointer to a pointer as a parameter isn't clear to
> me. Even after all these years with the Windows API, I don't see the
> purpose here. Why not just use a const char *?

The function has the side effect - it needs to change something in 
addition to returning a value.  That change can only be made if you pass 
the location of the object that needs to change.  If outside it's a 
pointer to const char that is expected to change (i.e. to start pointing 
to another const char), you have to pass that pointer either by 
reference or by its address.

> These attempts failed with compiler errors, when trying to update the
> str param
> str = str + indexOnePastDelimeter;             // If it was a const
> char *, this is what I'd do
> str =&(*str + indexOnePastDelimeter);      // I don't see why the
> compiler complains about this one
> str =&((*str)[indexOnePastDelimeter]);
>
>
> I am going to submit a more c++ implementation (below) with a
> different prototype, because I failed at updating the str parameter in
> all my attempts and the c-style implementation came out very ugly.
> However, if anyone can address those 2 issues for me, I'd like to know
> what the thinking was.

I am not sure what you'd like to get as an answer, perhaps you want to 
ask more questions, so do.

>[.. a solution to a problem with different requirements ..]
V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

pointer to a pointer problems Christopher <cpisz@austin.rr.com> - 2011-08-15 11:16 -0700
  Re: pointer to a pointer problems Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-15 15:40 -0400
    Re: pointer to a pointer problems Christopher <cpisz@austin.rr.com> - 2011-08-15 13:31 -0700
      Re: pointer to a pointer problems Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-16 08:41 -0400

csiph-web