const char pointers

C

crystal twix

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.
 
P

paperab

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;
}
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
 
C

crystal twix

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;
        }
        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 *.
 
P

Paul N

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.
 
J

James Kanze

On 2 Nov, 16:01, crystal twix <[email protected]> wrote:

[...]
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.)
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top