Click to See Complete Forum and Search --> : array / pointer error?


Speedi3579
November 30th, 2004, 04:03 PM
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:


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):




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.

Graham
November 30th, 2004, 05:15 PM
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.

Speedi3579
November 30th, 2004, 05:32 PM
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.

Kheun
November 30th, 2004, 07:19 PM
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.

DallasZimmer
November 30th, 2004, 10:16 PM
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:

char backwards[256];
cout << ReverseStr ("You're hungry, right?", backwards) << endl;

Cheers.

Graham
December 1st, 2004, 03:36 AM
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.

Bob Davis
December 1st, 2004, 06:41 AM
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.