0

i have a simple pointer to an array and i want to reverse that array, this is the code that i wrote:

void str_reverse(char *s) {
    int l, r;
    l = 0;
    r = str_len(s) - 1;
    char temp;
    printf("length %i ", r);
    while (l <= r) {
        temp = *(s + l);
        printf("\n%c [%i] | %c [%i]\n", temp, l, *(s + r), r);

        *(s + l) = *(s + r);
        *(s + r) = temp;
        r--;
        l++;
    }
}

As you can see i use this fuction:

int str_len(char *p) {
    int l = 0;
    while (*(p + l) != '\0') {
//        printf("%c %i", *(p + l),l);
        l++;
    }
    return l;
}

I order to get the length of my array, this is my little main

int main() {
    char *s = "Hallo world!";

    str_reverse(s);

    return 0;
}

And this is the output:

length 10 
H [0] | d [10]
Bus error: 10

Now i tried to only print the values, so in the while loop of str_reverse i commented this two lines:

*(s + l) = *(s + r);
*(s + r) = temp;

And this is the output:

H [0] | ! [11]

a [1] | d [10]

l [2] | l [9]

l [3] | r [8]

o [4] | o [7]

  [5] | w [6]

That is good! But how can i store them? Thank you in advance

2
  • This happens when you call Pointer =>> Array. A Pointer is not an Array and an Array is not a Pointer. Commented Jan 29, 2016 at 16:37
  • 1
    A pointer points to "something", but an Array don't. Inside the Array "something". Will be copied. That's all...so if you want to copy "something" inside a Pointer, then you need to dinamicaly allocate some memory an use that pointer to point there. Commented Jan 29, 2016 at 16:40

1 Answer 1

2

The problem is that in main, s points to a string literal. These literals are typically stored in read only memory, so they can't be modified.

Change s to an array initialized with a string literal and it will work.

char s[] = "Hallo world!";

Using dynamically allocated memory will also work:

#include <stdlib.h>
#include <string.h>

...

char *s = strdup("Hallo world!");
str_reverse(s);
free(s);
Sign up to request clarification or add additional context in comments.

5 Comments

@Francesco You can do it with a pointer, but it can't point to read only memory like char *s = "Hallo world!";. It's fine if it points to an array or to dynamically allocated memory.
@Francesco And what are those warnings?
string.c:56:15: warning: implicitly declaring library function 'strdup' with type 'char *(const char *)' char *s = strdup("Hallo world!"); ^ string.c:56:15: note: include the header <string.h> or explicitly provide a declaration for 'strdup' string.c:87:5: warning: implicit declaration of function 'free' is invalid in C99 [-Wimplicit-function-declaration] free(s); ^ 2 warnings generated.
@Francesco Then you need to include string.h and stdlib.h. See my edit.
Strdup is not a C standard library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.