Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - const char pointers

 
Thread Tools Search this Thread
Old 11-02-2009, 08:18 AM   #1
Default const char pointers


For an assignment, I am trying to write a string function that returns
a portion of the string. My parameter list is a const char word[],
int start, int end, char result[].

Being new to arrays and pointers, I'm not sure what is the way I want
to handle the const char word[] without getting compiler warnings of
"Assignment qualifier discards qualifiers from pointer target type. I
thought I could create a pointer named temp

int *temp = word;

and get the string by using the start and end parameters by
incrementing the pointer through the array and returning result. But
I'm kind of stuck on where to start. Thanks.


crystal twix
  Reply With Quote
Old 11-02-2009, 12:12 PM   #2
paperab
 
Posts: n/a
Default Re: const char pointers
On Nov 2, 8:18*am, crystal twix <jonwongfanc...@gmail.com> wrote:
> For an assignment, I am trying to write a string function that returns
> a portion of the string. *My parameter list is a const char word[],
> int start, int end, char result[].
>
> int *temp = word;


"word" is a "const char pointer" so you should assign it to something
like:

const char* temp = word;

But this has not solved your problem (only creates another pointer to
your original character sequence).

If you function has to write a portion of your sequence (it's not a
std string but a zero terminated string I suppose), you have to place
a 0x00 at the end of the new sequence (you are not allowed to modify
the original sequence I suppose again).

Solve it without using any std routine means you need a simple loop:

void subSequence(const char word[], const int start, const int end,
char result[]) {
int k = 0;
for(int i=start; i<=end; i++) {
result[k++]=word[i];
}
result[k]=0x00;
}

and you can call it with:

char result[11] = {0x00};
subSequence("ciaociaoFIVE5hello", 8, 12, result);

then in result you must have: "FIVE5"...

Cheers



paperab
  Reply With Quote
Old 11-02-2009, 04:01 PM   #3
crystal twix
 
Posts: n/a
Default Re: const char pointers
On Nov 2, 4:12*am, paperab <ab9380...@yahoo.co.uk> wrote:
> On Nov 2, 8:18*am,crystaltwix<jonwongfanc...@gmail.com> wrote:
>
> > For an assignment, I am trying to write a string function that returns
> > a portion of the string. *My parameter list is a const char word[],
> > int start, int end, char result[].

>
> > int *temp = word;

>
> "word" is a "const char pointer" so you should assign it to something
> like:
>
> const char* temp = word;
>
> But this has not solved your problem (only creates another pointer to
> your original character sequence).
>
> If you function has to write a portion of your sequence (it's not a
> std string but a zero terminated string I suppose), you have to place
> a 0x00 at the end of the new sequence (you are not allowed to modify
> the original sequence I suppose again).
>
> Solve it without using any std routine means you need a simple loop:
>
> void subSequence(const char word[], const int start, const int end,
> char result[]) {
> * * * * int k = 0;
> * * * * for(int i=start; i<=end; i++) {
> * * * * * *result[k++]=word[i];
> * * * * }
> * * * * result[k]=0x00;
>
> }
>
> and you can call it with:
>
> char result[11] = {0x00};
> subSequence("ciaociaoFIVE5hello", 8, 12, result);
>
> then in result you must have: "FIVE5"...
>
> Cheers


paperab, you are correct in that I thought about creating another
const char * to point to the const char *word[], but then I get
another const pointer so I didn't see how that would help me.

Daniel T, we are not allowed to use any functions from the string
library.

And I forgot to mention that we are not allowed to create any new
variables besides one pointer of type char *.


crystal twix
  Reply With Quote
Old 11-02-2009, 08:20 PM   #4
Paul N
 
Posts: n/a
Default Re: const char pointers
On 2 Nov, 16:01, crystal twix <jonwongfanc...@gmail.com> wrote:
> On Nov 2, 4:12 am, paperab <ab9380...@yahoo.co.uk> wrote:
> > On Nov 2, 8:18 am,crystaltwix<jonwongfanc...@gmail.com> wrote:

>
> > > For an assignment, I am trying to write a string function that returns
> > > a portion of the string. My parameter list is a const char word[],
> > > int start, int end, char result[].


> > > Being new to arrays and pointers, I'm not sure what is the way I want
> > > to handle the const char word[] without getting compiler warnings of
> > > "Assignment qualifier discards qualifiers from pointer target type. I
> > > thought I could create a pointer named temp


> > > int *temp = word;


This is a strange thing to want to do. Other people have given advice
but I suspect you are more confused than they think you are.

"word" is an array of characters, so you want to point at it with a
pointer designed to point at characters. For some reason you are
trying to use a pointer to int. This is unlikely to work properly.
Also, "word" is supposed to be an array that you read but you do not
write to. If you do "char *temp = word" then you are making "temp"
point at the array, even though temp is a pointer that you can write
to, giving you the opportunity to mess up the array that you promised
you wouldn't alter. This is what the compiler is complaining about.
You should listen to its complaints.

> And I forgot to mention that we are not allowed to create any new
> variables besides one pointer of type char *.


If this is truly what you are meant to be doing, it's going to be
tricky. One way would to use a pointer (let's call it p) to point to
the place in "result" that you are writing to; you could then use word
[ (p - result) + start ] to be the place you read from. But this seems
much more advanced than the level you are at at present.

Hope this is useful.
Paul.


Paul N
  Reply With Quote
Old 11-03-2009, 09:31 AM   #5
James Kanze
 
Posts: n/a
Default Re: const char pointers
On Nov 2, 8:20 pm, Paul N <gw7...@aol.com> wrote:
> On 2 Nov, 16:01, crystal twix <jonwongfanc...@gmail.com> wrote:


[...]
> > And I forgot to mention that we are not allowed to create
> > any new variables besides one pointer of type char *.


> If this is truly what you are meant to be doing, it's going to
> be tricky. One way would to use a pointer (let's call it p) to
> point to the place in "result" that you are writing to; you
> could then use word [ (p - result) + start ] to be the place
> you read from. But this seems much more advanced than the
> level you are at at present.


This was my initial impression, but in fact, he's passed a
pointer to the place where he should put the results, so it's
actually possible to solve the problem without any new
variables. (I'd probably use two new pointers, since it's
more or less idiomatic in C++ to use the STL iterator pattern.)

I suspect, however, that the solution the prof is looking for
treats the word and result as arrays, and indexes into them,
despite the fact that they really are pointers, and that such
use is not very idomatic in C++. (Given the assignment, I
suspect that the prof isn't very versed in C++ to begin with.)

--
James Kanze


James Kanze
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning - pump and dumpers sending out trojan pointers Tester Computer Support 2 08-19-2007 07:32 PM
Mouse pointers? Gorbag Computer Support 1 06-29-2005 08:26 PM
Newbie looking for pointers Tom Wireless Networking 2 10-17-2004 02:43 AM
Toolbar 'pointers' are 5's and 6's and 8's??? fredcromer Computer Support 1 04-21-2004 08:48 PM
Pointers also changing into NUMBERS !!!?? fredcromer Computer Information 1 04-21-2004 08:27 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46