I assume strrev
takes a char *
pointer to a null-terminated string and reverses the order of the char
elememts of the string in place. That means that it cannot be used to reverse a string literal because modification of a string literal results in undefined behavior.
OP's array s
contains pointers to string literals that are passed to strrev
, resulting in undefined behavior. To prevent that, the code needs to be changed so that the array s
contains pointers to modifiable strings. That can be done either by creating each string as a named array of char
, or by constructing each string as an anonymous compound literal.
Version using named arrays of char
:
static char s_0[] = "To err is hman..";
static char s_1[] = "man is wild";
static char s_2[] = "Dream big thought";
static char s_3[] = "do it myself self";
char *s[] = { s_0, s_1, s_2, s_3 };
Version using compound literals:
char *s[] = {
(char []){ "To err is hman.." },
(char []){ "man is wild" },
(char []){ "Dream big thought" },
(char []){ "do it myself self" },
};
In both of the cases above, the string literals are only being used to initialized arrays of char
that are modifiable. The pointers in array s
point to these modifiable arrays of char
, so there is no problem passing them to strrev
.
Regarding strrev
, that function is not defined by the C standard, but it might be an extended standard library function of some implementation. All function names beginning with str
, mem
, or wcs
are reserved by the C standard.