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

This is a discussion on how to allocate memory dynamically to the pointer used in scanf function? within the C Programming forums, part of the General Programming Boards category; Hi, Can anybody tell me how to allocate memory to a pointer which is to used in scanf. Code: char ...

  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,047
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 01:46 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08: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

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