473,508 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arbitrary String Length by Dynamic Memory Allocation to a Pointer

bobbi2004
2 New Member
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 !
Feb 17 '08 #1
4 2812
Arulmurugan
90 New Member
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.
Feb 17 '08 #2
bobbi2004
2 New Member
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 :)
Feb 17 '08 #3
ashitpro
542 Recognized Expert Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. do{
  2. ch=getche();
  3. //use this ch and form node of the link list by dynamic memory allocation
  4. }while(ch!=13);
  5.  
Feb 17 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Feb 17 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2326
by: John Smith | last post by:
Hi, I'm writing a library in C++ which is supposed to be used by people using C. One function I have must return a string to users which is arbitrary length. The user must be able to use this...
51
8211
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
4
8780
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
5
2946
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!)...
11
3026
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
19036
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
7945
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
2947
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
14
3812
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.