0

Recently I study C and there is what I can't understand.
I knew and found that gets( ) function returns char *.

char * gets(char *buffer)

#include <stdio.h>

int main(void)
{
    char str[20], *p;
    p = gets(str) /* <- gcc said warning: assignment ~. */
}

definitely p is char * type and the return type of gets( ) is also char *.
Then why gcc said warning:

assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion] ?

Well it's working... but I just expected compiling with no any messages.

Edit:

  1. char * gets(char *buffer) was exist but now removed from language.

  2. But it is still in library.

  3. Then ,with implicit declaration,gets is compiled with default return value int.

  4. Though gets works well, It's very dangerous to use. Now fgets much better choice than gets

In addtion, I wanna know that how gets is exist on header file.

#if __CLIBC_USE (DEPRECATED_GETS)
/* Get a newline-terminated string from stdin, removing the newline.

    This function is impossible to use safely. It has been officially removed from ISO c11 and ISO c++14, and we have also removed it from the _GNU_SOURCE feature list. It remains available when explicitly using an old ISO C, Unix, or POSIX standard.

    This function is a possible cancellation point and therefore not marked with __THROW. */
extern char *gets (char *__s) __wur __attribute_deprecated__;
#endif

I could understand #ifdef that comments told me and how function is declared to be used.

Thank you for everyone! (_ _ )

  • 4
    Never ***ever*** use the `gets` function. It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) it has even been removed from the C language. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. Any learning or teaching resource using `gets` should be looked at with great suspicion. – Some programmer dude Dec 06 '22 at 17:26
  • 6
    `gets()` has been removed from the language, you may be using a version that doesn't have it declared, so you're getting the default `int` declaration. – Barmar Dec 06 '22 at 17:26
  • @Barmar Was `gets` removed? Then how can I use the function that doesn't declared? – Sung-E-gkoght Dec 06 '22 at 17:37
  • 1
    It's still in the C library because it needs to be compatible with multiple versions of C. But depending on the version you're compiling for, it may be removed from the header file with `#ifdef`. – Barmar Dec 06 '22 at 17:39
  • 3
    Did you get another warning `implicit declaration of function 'gets'; `? – Jabberwocky Dec 06 '22 at 17:41
  • @Jabberwocky oh yes I also get that. – Sung-E-gkoght Dec 06 '22 at 17:44

1 Answers1

3

You certainly got another warning warning: implicit declaration of function 'gets';.

That means that the compiler didn't find the definition of gets (presumably because it has been removed from the language about 10 years ago). Therefore it assumes that gets is a function that returns an int and p = gets(str) would then assign an int to a pointer, hence the second warning assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast.

If you see warnings containing the word "implicit", consider them as an error.

And "makes pointer without a cast" warnings are almost always actually errors.

I suggest you use fgets instead, but be aware that with fgets your line ends usually with a \n (that can be removed easily, for more details read this: Removing trailing newline character from fgets() input)

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115