Thread: why gets doesn't work with the second one

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    why gets doesn't work with the second one

    Halo all,
    I'm posting here after a long time. During this time I was just sharpening my pointer skills but as pointers are the most "easiest" part of C I got stuck at this one.
    Why does gets(although it's not advisable) not work with the second one but with the first.
    Code 1:
    Code:
    char s[5];
    gets(s);
    Code 2:
    Code:
    char *s;
    gets(s);
    As I see it s points to first element in both the cases, and both of them not initialized at all. So s doesn't points to a particular address initially.
    Thanks
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Declaring a pointer does not implicitly create something for that pointer to point at.

    In "Code 2", s is an unitialised pointer, which means it points at some random memory location. gets(s) therefore overwrites that random area of memory. Formally, the result is undefined behaviour - anything is allowed to happen (a program crash is a common result, but other results are possible).

    "Code 1" will exhibit undefined behaviour if the data read on a line from stdin exceeds 4 characters in length - because it will run past the end of the array. If the input on a line does not exceed 4 characters, all is fine.
    Last edited by grumpy; 06-23-2009 at 07:13 AM.
    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
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Here's my real doubt. Isn't 's' an uninitialized pointer in code 1 coz 's' is the base address of the array. Also I changed my code 2 like this:
    Code 2:
    [CODE]
    char *s="hello";
    gets(s);
    [\CODE]
    But it still gives me run time error. What I think here is 's' is now initialized pointer so it should be working properly.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, now the problem is that s is a pointer to the first element of a string literal, thus you cannot modify what s points to without resulting in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by laserlight View Post
    Well, now the problem is that s is a pointer to the first element of a string literal, thus you cannot modify what s points to without resulting in undefined behaviour.
    Then how does this works?
    Code:
    char *s="hello";
    s="bye";
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BEN10
    Then how does this works?
    You are changing the pointer, not what the pointer points to. Oh, and if s is supposed to point to some character in a string literal, it should be a const char* rather than a char*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by BEN10 View Post
    Here's my real doubt. Isn't 's' an uninitialized pointer in code 1 coz 's' is the base address of the array.
    You said it yourself: "'s' is the base address of the array" Therefore, 's' isn't uninitialized, it represents the address of the first element of the array in this context.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by hk_mp5kpdw View Post
    You said it yourself: "'s' is the base address of the array" Therefore, 's' isn't uninitialized, it represents the address of the first element of the array in this context.
    It sounds funny but where's the first element of the array? I mean 's' points to the first element of the array but the first element is not present so in effect 's' points to a random memory location just like char *s does. So how come gets work with it properly?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BEN10
    It sounds funny but where's the first element of the array?
    In the first example, it is at the first position in the memory allocated for the array.

    Quote Originally Posted by BEN10
    I mean 's' points to the first element of the array but the first element is not present
    In the first example, the first element (and indeed all the elements) of the array exists. Its value is another matter.

    Quote Originally Posted by BEN10
    so in effect 's' points to a random memory location just like char *s does.
    s is an array, not a pointer. When s is converted to a pointer, that pointer points to the first element of s.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Thanks laserlight. You cleared a whole lot of doubts from my mind. Now I get exactly what's the differnce between char *s and char s[5]. Thanks once again.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    I tried to use your code using codeblocks with mingw and it worked... I wonder what error you're encountering.?


    edit: nevermind it's now resolved ...
    Last edited by $l4xklynx; 06-23-2009 at 09:15 AM. Reason: nvm

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by $l4xklynx View Post
    I tried to use your code using codeblocks with mingw and it worked... I wonder what error you're encountering.?


    edit: nevermind it's now resolved ...
    I dont know how your compiler ran the code but my error was
    The variable 's' is being used without being defined.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM