Why is this string reversal code not working?

Why is this string reversal code not working?


#include
#include

void reverse(char* str) {
if (str) {
char* start;
char* end;
char temp;

start = str;
end = str;
while (*end != '\0') {
end++;
}
end--;

while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
}

int main() {
char* str = "Hello World!";
reverse(str);

printf(str);

getchar();
}

I tried everything. What's wrong?

Other urls found in this thread:

cdecl.org/
twitter.com/SFWRedditVideos

Omg I fixed it thnx Sup Forums
Solution: I am retarded

>while (*end != '\0') {
>end++;
>}
>end--;

UNIRONICALLY USING VISUAL STUDIO

So I keep moving end forward until it becomes equal to the null character, then I pull it back once to point to the last character in the string. Is there something I am not seeing?

I hate having to use visual studio, anyone wanna recommend something else with a c++ window builder? all the ones on google fucking cost money.
>>poorfag yes

you're trying to write to read only memory
the memory for "hello world" is not writable
use strdup and don't forget to free

OP Here.

When I replace

> char* str = "Hello World!";

with

> char str[] = "Hello World!";

it works fine. You are right!

>if (str) {
>the entire function
>}

Brainlet Lmao, check your pointer, it should be an array....imbecile

use strlen

you're a faggot, as it is always the case for tripfags

Look at this filth.

lel

Here, I optimized for you:
#include

void main(){
printf("!dlroW olleH");
}

DON'T
EVER
USE
strlen
EVER

use strnlen instead. It's safe.

Just stick to Java, my street-shitting friend.

Next time use [ code ] [ /code ] tags

unsigned int length(char* str) {
unsigned int len = 0;

while(str) {
len++;
}
len--;

return len;
}

Tell my how shit my function is, its been a long time since Ive done anything. I feel like this wouldnt even compile. maybe think of it as a concept or blueprint if that helps you hate me less.
aaaaaaaaa(stuff){
char* adsf[strlen(str)] = str;
int a=0;
for(int i=strlen(str); i>0; i--){
asd[a] = str[i];
a++;
}

> if (str) {
...
> while (*end != '\0') {
> end++;
> }
> end--;

> these are the brainlets trying to take my job

o i am laffin

you're trying to declare an array of pointers with a runtime length

Yea, I had a feeling that wouldnt work. I also didnt need to put =str on there. iirc that never worked either.
I also feel my variable types dont even work the way i was trying to use them

cdecl.org/
if you're ever in doubt

What's wrong with this?

there's a standard function for this

Ill check it out, seems neat thanks.

Is copying a character like this invalid?
char array1[5]
char array2[5]

array2[i] = array1[i];

It wont fucking work with gcc or g++, yet Im looking at some code I wrote years ago and it works with mingw

Which is?