|
-
November 30th, 2004, 05:03 PM
#1
array / pointer error?
Hello,
I am writing a program that uses my own function to reverse a string and print the reverse. I am experiencing an error that I just don't know why it is occuring. Here is the code block, and the error:
Code:
int main()
{
char myString[] = "aBc123";
char revString[80] = "1";
char *ch;
ch = ToUpperStr(myString);
revString = ReverseStr(myString, revString); // error, left operand must be l-value
printf("%s\n%s\n", myString, ch);
return 1;
}
and for clarification, here is my ReverseStr() function code(Note: it currently doesn't reverse the string, I'm holding off on writing that part until I get this current little error fixed):
Code:
char* ReverseStr(char * str, char * reverseStr)
{
// char *strStart = str;
char *revStart = reverseStr;
while (*str)
{
*reverseStr = *str;
str++;
reverseStr++;
}
reverseStr = revStart;
return reverseStr;
}
Does anyone know why my function can't return the address properly to revString? Is there something I am missing here? I want my function to return the address of the modified string, and the function should be passed the address that I want to place the modified string at.
Thanks in advance for your help,
Alan.
'Grat is not nice, indeed!
-
November 30th, 2004, 06:15 PM
#2
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 30th, 2004, 06:32 PM
#3
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.
'Grat is not nice, indeed!
-
November 30th, 2004, 08:19 PM
#4
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.
-
November 30th, 2004, 11:16 PM
#5
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.
Just remember that it is always darkest just before it goes pitch black.
-
December 1st, 2004, 04:36 AM
#6
Re: array / pointer error?
 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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
December 1st, 2004, 07:41 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|