Arbitrary String Length by Dynamic Memory Allocation to a Pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bobbi2004
    New Member
    • Feb 2008
    • 2

    Arbitrary String Length by Dynamic Memory Allocation to a Pointer

    Well, thank you for reading this question . Any answers or ideas are very appreciated .

    I am new in C and i have a proplem . I want to write a program that accept some strings from the users , i want to use Dynamic Memory Allocation to a pointer depending on the length of the string entered by the user.

    Here i have had a search in this forum and i found one solution is to ask the length of the string in advance
    : Link To This Post. But can i have any other solution that i dont have to ask any thing, just let the users enter their strings and use it to allocate memory dynamically.

    Thank you very much !
  • Arulmurugan
    New Member
    • Jan 2008
    • 90

    #2
    Originally posted by bobbi2004
    Well, thank you for reading this question . Any answers or ideas are very appreciated .

    I am new in C and i have a proplem . I want to write a program that accept some strings from the users , i want to use Dynamic Memory Allocation to a pointer depending on the length of the string entered by the user.

    Here i have had a search in this forum and i found one solution is to ask the length of the string in advance
    : Link To This Post. But can i have any other solution that i dont have to ask any thing, just let the users enter their strings and use it to allocate memory dynamically.

    Thank you very much !
    Hello,
    You can declare very big array( eg char line[bing number]), then always get the i/p from line, findout the length and allocate memory, then finally copy line to you alllocated memory.

    Regards,
    Arul.

    Comment

    • bobbi2004
      New Member
      • Feb 2008
      • 2

      #3
      Originally posted by Arulmurugan
      Hello,
      You can declare very big array( eg char line[bing number]), then always get the i/p from line, findout the length and allocate memory, then finally copy line to you alllocated memory.

      Regards,
      Arul.
      Immediate reply !! :) :) thank you.

      Yeah that 's one solution too, but i am wondering are there other more efficient solutions (dont have to use an array).

      anyway , thank you for your reply :)

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        Originally posted by bobbi2004
        Well, thank you for reading this question . Any answers or ideas are very appreciated .

        I am new in C and i have a proplem . I want to write a program that accept some strings from the users , i want to use Dynamic Memory Allocation to a pointer depending on the length of the string entered by the user.

        Here i have had a search in this forum and i found one solution is to ask the length of the string in advance
        : Link To This Post. But can i have any other solution that i dont have to ask any thing, just let the users enter their strings and use it to allocate memory dynamically.

        Thank you very much !
        If you really want to do this, use link list.
        algo should look like this:
        Code:
        do{
        ch=getche();
        //use this ch and form node of the link list by dynamic memory allocation
        }while(ch!=13);

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          To get a string of indeterminate length, use getline and a small buffer, say 20 bytes.

          getline() returns the number of characters read. If it returns 20, you haven't got the entire string. So, append the buffer to another one that is at least 20 bytes. Then do the getline() again, and again until the number of characters read is less than 20 each time appending to the other buffer.

          Functions like strcat() allow you to append to an existing buffer.

          Comment

          Working...