1

While compiling this below given code, I got the error: lvalue required as left operand of assignment for both the statements str+i = str+((len-1)-i); and str+((len-1)-i) = temp;.

Why?

char* str_reverse(char *str, int len)
{
    char *temp;
    int i, mid;

    if(len%2 == 0)
    {
        mid = len/2 - 1;
    }
    else
    {
        mid = (len-1)/2;
    }

    for(i=0;i<mid;i++)
    {
        temp = str+i;
        str+i = str+((len-1)-i);
        str+((len-1)-i) = temp;
    }

    return str;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Because `str+i` is not an lvalue (see for example [What is an lvalue](https://stackoverflow.com/questions/6397745/what-is-an-lvalue)). The statement doesn't swap characters as you intend it to. Should be `*(str+i)=*(str+((len-1)-i))`. Or more simply `str[i] = str[len-1-i]`. Same for all the other assignments – kaylum Oct 24 '21 at 19:23
  • Sorry, I forgot to mention this: I wanted to assign the address of ```(str+((len-1)-i))``` to ```str+i``` , not the value stored in it. –  Oct 24 '21 at 19:26
  • Why? That makes no sense. That will not swap the characters at all. – kaylum Oct 24 '21 at 19:26
  • I wanted to reverse the order of characters stored in pointer ```str```. So, I thought to **re-allocate their memory addresses** in such a way that: calling ```str[0]``` would would return the value of ```str[(n-1)-0]```. calling ```str[1]``` would would return the value of ```str[(n-1)-1]```. calling ```str[2]``` would would return the value of ```str[(n-1)-2]```. . . . and so on. (I don't know whether this method works, I'm new to programming, hence I don't know much of its rules.) I wanted to know that, if this statement doesn't serve my purpose, Why? –  Oct 24 '21 at 19:41
  • @Algorithmophile You can't individually reallocate the addresses of the elements of an array. It's the definition of an array that its elements are all stored in contiguous memory. So the address of every element of an array is always at a fixed offset from the base address, that is, the beginning of the array. If you want to rearrange the elements of an array, rearranging the values is your only choice. – Steve Summit Oct 24 '21 at 19:58
  • Even if you *could* rearrange the addresses of the individual elements of an array, moving 4- or 8-buyte addresses around, to avoid the overhead of moving single characters around, would probably not be a good tradeoff! :-) – Steve Summit Oct 24 '21 at 20:02
  • Okay. I thought re-allocating addresses (if it is possible) would be faster than re-allocating the values. I was trying this bounty question https://stackoverflow.com/q/34035169/15933960 . So, I thought about this method. Thank you all for support :) –  Oct 24 '21 at 20:09

3 Answers3

2

str is an lvalue because it is a place in memory which contains an address.

str+i is only an rvalue because it is only an address

Robin
  • 354
  • 1
  • 13
0

Expressions like this

str + i

are rvalues. They can not be assigned. It is the same as to write

int x = 10;
int y = 20;

x + y = 30;

But in any case your function is in essence incorrect. You need to swap characters instead of trying to swap pointer expressions.

Also this if statement

if(len%2 == 0)
{
    mid = len/2 - 1;
}
else
{
    mid = (len-1)/2;
}

does not make a great sense because the result of the expression

len / 2

is the same whether len is an even number or the following odd number.

For example 2 / 2 is equal to 1 and 3 / 2 is also equal to 1.

Using pointers within the function it can be defined the following way

char * str_reverse( char *str, size_t len )
{
    if ( len != 0 )
    {
        for ( char *left = str, *right = str + len; left < --right; ++left )
        {
            char c = *left;
            *left = *right;
            *right = c;
        }
    }

    return str;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Use size_t for string lengths.

You are overcomplicating this:

char* str_reverse(char *str, size_t len)
{
    char temp, *start = str, *end = str + len -1;
    if(str && *str)
    {
        while(start < end)
        {
            temp = *start;
            *start++ = *end;
            *end-- = temp;
        }
    }
    return str;
}
0___________
  • 60,014
  • 4
  • 34
  • 74