So I'm new to C and I'm playing around with functions in the GNU C Library when I come across https://www.gnu.org/software/libc/manual/html_node/strfry.html#strfry
Intrigued, I wrote a little tester program:
1 #include <stdio.h>
2 #include <string.h>
3
4 main ()
5 {
6 char *str = "test123abc";
7 char *other;
8
9 other = strfry(str);
10 printf("%s\n", other);
11 return 0;
12 }
gcc test.c
outputs test.c:9: warning: assignment makes pointer from integer without a cast
Why?
/usr/include/string.h
has the following entry:
extern char *strfry (char *__string) __THROW __nonnull ((1));
How can a char *function(...)
return int
?
Thanks
Since strfry
is a GNU extension, you need to #define _GNU_SOURCE
to use it. If you fail to provide that #define
, the declaration will not be visible and the compiler will automatically assume that the function returns int
.
A related problem, as pointed out by perreal, is that it is undefined behavior to modify a literal string. Once you make the declaration of strfry
visible to the compiler, this will be duly reported.
Do note that the strfry
function and its cousin memfrob
are not entirely serious and are rarely used in production.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With