Thread: how to allocate memory dynamically to the pointer used in scanf function?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    22

    how to allocate memory dynamically to the pointer used in scanf function?

    Hi,
    Can anybody tell me how to allocate memory to a pointer which is to used in scanf.

    Code:
    char *varpointer;
    scanf("%s" , varpointer); // read some chars here
    when i used above code fragment in my program it returns segmentation error. I suppose its due to not allocating memory for varpointer.

    So please advice me how to allocate memory to varpointer ?

    appreciate your time.
    thanks.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    have you not heard of the malloc() family!

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    22
    Hi,
    I know how to use malloc function basically.
    what i want to know is how to allocate the memory dynamically with scanf.
    for example say im going to scan the string "Test" . Then i want to allocate the length of "Test".
    If Im going to scan different string like "Something Else" then the length of it.
    hope it make sense.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by wwwnuwan View Post
    Hi,
    I know how to use malloc function basically.
    what i want to know is how to allocate the memory dynamically with scanf.
    for example say im going to scan the string "Test" . Then i want to allocate the length of "Test".
    If Im going to scan different string like "Something Else" then the length of it.
    hope it make sense.
    scanf does not work that way - it uses buffer you provide

    if you want the buffer dynamically grow during read of the string - write your own function that will realloc as soon as buffer is filled and the new line character still not encountered

    PS. you will read using fgetc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Plus reading strings with scanf() is a bad idea. Buffer overflows here we come.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM