Thread: Question about gets()

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    1

    Unhappy Question about gets()

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    
    
    char address;
    printf("Enter your address: ");
    gets(address);
    printf("Your address is %s", address);
    return 0;
    }
    I really have no idea why there's a 'warning' in this code..
    Any help plz..?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The mostly like reason is that gets() expects to receive a pointer which is the address of the first element of an array. It does not expect what you're doing - supplying the value of a single char.

    The same goes for the %s format specifier for printf.()


    If your compiler is fairly modern, it may also complain about using gets() AT ALL, since gets() was REMOVED from the latest C standard. This is because gets() is dangerous and there is no way for a programmer to make its usage safe. Look up the fgets() function for an alternative.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    15
    I'm pretty new to programming and a book said never use gets() in real life, but for an example use it... I understand that, but my compiler (gcc) gave me a segmentation fault when I ran it. So I had to use scanf(). Any ideas on how to fix that?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I understand that, but my compiler (gcc) gave me a segmentation fault when I ran it
    Well that's what you get for ignoring warnings, and passing garbage into functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    In your code, address is a single character, when what you need is an array. Make address a char array of size 50 (for example), and everything should start to work.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    I will add also that the use gets is highly not recommended. Use fgets instead. Information on this is everywhere, even in Wikipedia.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by yuxuantim View Post
    So I had to use scanf(). Any ideas on how to fix that?
    Did you even bother to read my previous post?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Okay so...

    You have declared "address" as a char.

    This reserves enough space for one letter

    Code:
    address = 'A';
    gets() tries to put more than one letter at the variable "address": Your computer sees your program trying to modify memory that it doesn't have permission to modify, so it throws a segfault.

    Here is a tutorial on how to reserve more than just one char
    Arrays in C - Cprogramming.com

    Here is a tutorial on strings (which is what you are after) which has an fgets() example.
    C Strings - Cprogramming.com

    A string (in the C language) is an array of chars where the final character is a Nul '\0' character.
    (Note that this is not NULL with 2 L's, as NULL is a pointer which points to nothing FAQ > NULL, 0, \0 and nul? - Cprogramming.com)

    The string "Hello" would be stored in an array as,
    Code:
    {'H', 'e', 'l', 'l', 'o', '\0'}
    , and the array it is stored would have to have a size of at least 6.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. java question, how do you interpret this question.
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-02-2006, 10:30 AM
  5. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM