Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strfry(char *__string) returns int?

Tags:

c

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

like image 464
JB0x2D1 Avatar asked Dec 09 '22 17:12

JB0x2D1


1 Answers

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.

like image 143
user4815162342 Avatar answered Dec 22 '22 02:12

user4815162342