473,664 Members | 2,759 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 2822
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
2361
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 string as "char *" so I was wondering if the following construct is safe: char *Func() { static string str;
51
8261
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 code? many thx!
4
8807
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
5
2959
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!) printing the results I expect, which I guess means my dynamic memory allocation is a mess. Also, I was advised previously that I should really free memory in the same place I declare it, but I'm not sure how I would go about doing this in my code...
11
3040
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
19070
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
1
7963
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
3
2986
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 facing: 1- From structure and dynamic memory allocation point of view, the code is very complicated. The code has various “nested structures” with a number of levels. The size of memory allocated for pointer to structure or its pointer...
14
3823
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 this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
0
8438
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4186
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.