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.
So something like:
void myFunc(const char word[], int start, int end, char result[]);
I suggest you start simple:
int main() {
char result[32] = 0;
myFunc("", 0, 0, result);
assert(strcmp(result, "") == 0);
// once you get the above working then:
myFunc("hello", 0, 1, result);
assert(strcmp(result, "h") == 0);
myFunc("world", 0, 1, result);
assert(strcmp(result, "w") == 0);
// then make sure your edge conditions work
myFunc("hello", 4, 5, result);
assert(strcmp(result, "o") == 0);
myFunc("world", 4, 5, result);
assert(strcmp(result, "d") == 0);
}
Get all the above working, if you run into trouble, then post what you
have so far here and I'll help you out.
> 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
You will need to use separate pointers that access "word", and ones that
access "result" (if any).
Declare the pointers that access "word" as "const char *", instead of "char
*". If you need a pointer to do whatever you need with "result", that's
still a plain "char *".
"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 *.
> Daniel T, we are not allowed to use any functions from the string
> library.
I never suggested you do anything of the sort. What I'm suggesting is
that you break the problem up into smaller sub-problems. Start with
something easy to solve, then solve it, then something a little harder
and solve it, and so on.
Here is some interesting code that should help you...
int main() {
const char var[] = "hello world";
const char* p = var;
assert(*p == 'h');
p += 6;
assert(*p == 'w');
p += 5;
assert(*p == 0);
}
> And I forgot to mention that we are not allowed to create any new
> variables besides one pointer of type char *.
You don't need any new variables at all to solve the problem, you
already have everything you need. Extra variables are needed for error
controls maybe but that's it.
> > > 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.
[...]
> > 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