-3

I'm using gets() cause it's a jomework and we are told to. It acts like I'd have no gets(&a)...

    ` char a,b,c;
      switch(x);
            case '1':printf("Please enter the author\n");
                     printf("Author:");
                     gets(&a);
                     printf("\nTitle:");
                     gets(&b);`
  • Can you show the definition for the `a` variable? `gets()` expects a character buffer, and it's rarely called in this way (with the `&` address-of operator). – Steve Friedl Nov 09 '19 at 17:07
  • Hey there Laszlo and welcome to StackOverflow! Could you provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) of your situation so we can help you better? :) – Pedro Martins de Souza Nov 09 '19 at 17:07
  • You might want to rethink even using `gets`. See this post https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – StoryTeller - Unslander Monica Nov 09 '19 at 17:09
  • The first thing you need to know about `gets()` is never to use `gets()`. It is long ago deprecated and before that one of the C libraries most embarrassing errors. – Clifford Nov 09 '19 at 17:14
  • No, do not post code in comments. [edit] your post to contain a [mre] instead. – StoryTeller - Unslander Monica Nov 09 '19 at 17:14
  • Code::Blocks includes a debugger - you should use it. I am willing to bet that it runs `a` is assigned the empty string `""`. Whatever preceding input function call you have used (probably the one that accepts the `'1'` you are switching on) did not consume the newline ( `scanf( "%c"...)` or `getchar()` perhaps), so the next input function returns immediately because there is input already in the buffer. – Clifford Nov 09 '19 at 17:19
  • 1
    In any case, you will need a buffer longer than one char to input any kind of useful string:( – Martin James Nov 09 '19 at 17:29

1 Answers1

1

Why doesn't my first gets() work?

gets(char *); expects a pointer to a place to form a non-zero length string.

char a in only big enough for a zero length "" string.

gets(&a); is buffer overflow - research undefined behavior.


"I'm using gets() cause it's a jomework and we are told to." --> Sorry your school/work is so. Recommend researching for a better school.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256