Re: array / pointer error?
You can pass an array as the parameter to a pointer argument, but you can't assign a pointer to an array. You can think of your declaration char revString[80] as equivalent to char * const revString, that is, a constant pointer to char. Since it's constant, you can't assign the return value of your function to it. Why are you doing it that way, anyway? You're already modifying revString via the function argument, so there's no need to return it through the function return value.
Re: array / pointer error?
I see, thanks a bunch. Im doing it this way because my CS teacher is a little odd.. Despite wanting our functions to modify the string, he wants them to return a pointer to it, as well. Here were his exact rules for that function:
Write a function named "ReverseStr" which reverses the string front to back.
For Example, for the string "You're hungry, right?", the result would be "?thgir, yrgnuh er'uoY".
The function expects the original string and the address to place the resulting string, in that order.
The function returns a pointer to the resulting string.
Assume that the two blocks of memory don't overlap.
-end-
A little odd..but I have to do it his way or I'll probably get marked off a bunch.
Thanks again for your help. =)
Alan.
Re: array / pointer error?
Probably, your teacher is looking for your ReverseStr() to allocate memory and then copy the result into the new memory. Thus your function has to return the pointer. If so, you need to be aware that you have to release the memory when it is no longer needed.
Re: array / pointer error?
There are sometimes good reasons for functions to return a value that they are also modifying via the argument list.
Take your ReverseStr function for example: By returning the address of the reversed string, you can do things like this:
Code:
char backwards[256];
cout << ReverseStr ("You're hungry, right?", backwards) << endl;
Cheers.
Re: array / pointer error?
Quote:
Originally Posted by Speedi3579
I see, thanks a bunch. Im doing it this way because my CS teacher is a little odd.. Despite wanting our functions to modify the string, he wants them to return a pointer to it, as well. Here were his exact rules for that function:
Write a function named "ReverseStr" which reverses the string front to back.
For Example, for the string "You're hungry, right?", the result would be "?thgir, yrgnuh er'uoY".
The function expects the original string and the address to place the resulting string, in that order.
The function returns a pointer to the resulting string.
Assume that the two blocks of memory don't overlap.
-end-
A little odd..but I have to do it his way or I'll probably get marked off a bunch.
Thanks again for your help. =)
Alan.
In which case don't bother trying to reassign through the return value - just ignore it unless you're doing something like DallasZimmer has suggested and using the return value for a different reason.
Re: array / pointer error?
It's not just your CS teacher that's a bit odd. Most of the standard C library string functions return a pointer to the string that they're modifying. I've never been able to understand the utility of doing this, but that's how it is.