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 !
4 2812
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.
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 :)
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: -
do{
-
ch=getche();
-
//use this ch and form node of the link list by dynamic memory allocation
-
}while(ch!=13);
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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!)...
|
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;
}
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
| |