Reverse a string

Why is the reverse function giving me error while running it?
It is right at the assignment *p = *len statement.

void reverse(char *str) {
      char *len = &str[strlen(str)-1];
      char tmp;
      for (char* p = str; p < len; p++, len--) {
            tmp = *p;
            *p = *len;
            *len = tmp;
      }
}

int main(int argc, char* argv[])
{
      reverse("barbeque");
      return 0;
}
chunichAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brettmjohnsonCommented:
The string "barbeque" is a string constant.  Many compilers store string constants in read-only memory, so when you try to re-arrange the characters you get a memory access violation.  Rather than trying to modify a string constant, modify a mutable string that has been initialized to that constant value.

int main(int argc, char* argv[])
{
     char str[] = "barbeque";
     reverse(str);
     return 0;
}
bachra04Commented:
You access a non permitted memory, you should allocate enough space to do the reverse using static or dynamic memory allocation.

please try the following code, it should work:

#include <stdio.h>
#include <string.h>

void reverse(char *str) {
     char *len = &str[strlen(str)-1];
     char tmp;

     for (char* p = str; p < len; p++, len--)
       {
          tmp = *p;
          *p = *len;
          *len = tmp;
     }
}

int main(int argc, char* argv[])
{
       char szTemp[10]= "barbeque";
       reverse(szTemp);
       printf ("%s\n", szTemp);
     return 0;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jhanceCommented:
By the way, while it's not a part of the std. "C" RTL, many implementation (in fact MOST) have an implementation of a string reversal function.  In MS Visual C it's _strrev().  Most others use strrev() or similar.  In my opinion it's best NOT to re-invent the wheel if you can help it.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

brettmjohnsonCommented:
jhance,

The "Reverse a string" problem is a very common academic assignment, used to teach the use of pointers, arrays, and C strings.  The students are discouraged from using the library routine to accomplish the task, since they really would not learn anything.
jhanceCommented:
They also don't learn much by getting their homework done by experts here.  But that's another story...
bachra04Commented:
//They also don't learn much by getting their homework done by experts here.  But that's another story...

This is not the case in this thread, the algorithm proposed by the student is correct but he has only a problem of memory allocation and need help just at this point  so 90% of the job is done by him self and not by experts and I think that's correct .

B.T
chunichAuthor Commented:
Thank you guys so much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.