scanf and printf

This is a discussion on scanf and printf within the C Programming forums, part of the General Programming Boards category; hi all I am a little bit confused with this 'simple' code: - Code: #include <stdio.h> void main() { char ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    scanf and printf

    hi all

    I am a little bit confused with this 'simple' code: -

    Code:
    #include <stdio.h>
    
    void main()
    {
         char    *name = NULL;
    
         printf("Enter a name: -");
         scanf("%s", name);
    
         printf("\n%s", name);
    
    }
    why does this not work?
    I thought I understood pointers!

    Regards

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Most important thing to note: Main ALWAYS returns an int, even if your compiler lets you get away with it as void.

    Secondly, you need to allocate memory for your pointer, else you're just over writing random memory.

    Code:
    #include <stdio.h>
    
    int main()
    {
         char  *name;
         name = (char *)malloc(12);
         printf("Enter a name: -");
         scanf("%s", name);
         printf("%s\n", name);
         return 0;
    }
    I like to play pocket pool.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    First up, main returns an int. Second of all, name only allocates space for a pointer - nothing more. If you want to use name the way you have declared it, you would want to dynamically allocate some space. Otherwise, you could declare a static array something like so:

    Code:
    char name[NAME_MAX];
    Where NAME_MAX is a constant for the size you need the array to be.

    Also, your call to scanf is about as dangerous as a call to gets would be - ie., there is no way to limit user input, and therefore could easily overrun the space you have allocated for name. Have a look at the FAQ for some ideas on how to get text from a user.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > name = (char *)malloc(12);
    Also read the FAQ on casting malloc in C.
    Like including the correct header file.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You can limit the amount read to scanf:
    Code:
    scanf("%10s", name);
    Be careful in that you need to allocate 1 more than the amount passed to scanf, since the limit doesn't include the null character for termination of the string.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    And that code won't allow spaces in the inputted name.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 07:35 PM
  4. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-10-2002, 11:40 PM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21