-2

The length of cstr in my code is 3 from strlen(). I am doing a dereference on index 0 of cstr but it is giving me a segmentation fault on that line (line 34). I'm really confused. Can someone explain why this is happening?

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

char *reverse_words(char *cstr);

int main() {
    char *ex = "cat";
    printf("%s", reverse_words(ex));
    return 0;
}

char *reverse_words(char *cstr) {
  int len = strlen(cstr);
  int s_len = 0;
  int s_len_prev = 0;
  while (s_len < len) {
    while (cstr[s_len] != 32 && s_len < len) {
      s_len++;
    }
    printf("len: %d\n", len);
    printf("s_len: %d\n", s_len);
    
    for (int i = s_len_prev; i < s_len; i++) {
      printf("%d\n", i);
      if (i == (s_len / 2)) {
        break;
      }
      char temp1 = cstr[i];
      char temp2 = *(cstr + s_len - i - 1);
      printf("*(cstr + i): %c\n", cstr[i]);
      printf("*(cstr + s_len - i - 1): %c\n", *(cstr + s_len - i - 1));
      printf("s_len - i - 1: %d\n", s_len - i - 1);
      printf("len: %d\n", len);
      *(cstr + i) = temp2;
      *(cstr + s_len - i - 1) = temp1;

    }
    s_len++;
    s_len_prev = s_len;
  }
  return cstr;
}

OUTPUT:

len: 3                                                                                                                                                                                                                          
s_len: 3                                                                                                                                                                                                                        
0                                                                                                                                                                                                                               
*(cstr + i): c                                                                                                                                                                                                                  
*(cstr + s_len - i - 1): t                                                                                                                                                                                                      
s_len - i - 1: 2                                                                                                                                                                                                                
len: 3                                                                                                                                                                                                                          
Segmentation fault (core dumped)   
  • 1
    Welcome to Stack Overflow; you're more likely to get some help if you show the actual code in the body of your post - offsite images of code (which we cannot cut and paste) almost never get any interest. – Steve Friedl May 05 '21 at 00:38
  • 3
    Could be the old "trying to change a lteral string" trick. (That's twice this week :) - try `char ex[] = "cat";` instead of `char*ex = "cat";` – John3136 May 05 '21 at 00:40
  • thanks I got it working after changing to char ex[] = "cat"; – script-jpg May 05 '21 at 00:42

2 Answers2

0

You make ex point to a constant (called a "string literal"). Then you try to modify the thing ex points to. But you can't modify a constant -- that's what makes them constants.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

This is undefined behaviour in C, because "cat" could be placed on some read-only region of the executable. Some compilers won't allow char* cstring = "text" if you don't specify the pointer as const to avoid this error. By making it an array char ex[] you guarantee that it is writable. You can read more about it here Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?

user14789259
  • 423
  • 2
  • 11