473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dynamic allocation for text input

I'm trying to accept arbitrary length strings from stdin. I know that you can't do that automatically in C, so I ask the user to input the size of each string, allocate the memory for it, and then read the string.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6.     char *source_string = NULL;
  7.     char *search_string = NULL;
  8.     int source_length = 0, search_length = 0;
  9.  
  10.     puts("How long is your source string?");
  11.     scanf("%d", &source_length);
  12.     source_string = (char *)malloc(source_length + 1);
  13.     puts("Please enter your source string.");
  14.     scanf("%s", &source_string);
  15.  
  16.     puts("How long is your search string?");
  17.     scanf("%d", &search_length);
  18.     search_string = (char *)malloc(search_length + 1);
  19.     puts("Please enter your search string.");
  20.     scanf("%s", &search_string);
  21.  
  22.     printf("Now searching string \"%s\" for characters \"%s\".\n", &source_string, &search_string);
  23.  
  24.     puts("Cleaning up memory...");
  25.     free(source_string);
  26.     free(search_string);
  27.  
  28.     return EXIT_SUCCESS;
  29. }
  30.  
Unfortunately, it doesn't work properly. I've included a sample run below.

How long is your source string?
3
Please enter your source string.
123
How long is your search string?
6
Please enter your search string.
123456
Now searching string "56" for characters "123456".
Segmentation fault

Thanks in advance for any help!
Feb 13 '08 #1
2 2504
gpraghuram
1,275 Expert 1GB
Check this.
I have corrected few issues in the code
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6.     char *source_string = NULL;
  7.     char *search_string = NULL;
  8.     int source_length = 0, search_length = 0;
  9.  
  10.     puts("How long is your source string?");
  11.     scanf("%d", &source_length);
  12.     source_string = (char *)malloc(source_length + 1);
  13.     puts("Please enter your source string.");
  14.     scanf("%s", source_string);
  15.  
  16.     puts("How long is your search string?");
  17.     scanf("%d", &search_length);
  18.     search_string = (char *)malloc(search_length + 1);
  19.     puts("Please enter your search string.");
  20.     scanf("%s", search_string);
  21.  
  22.     //printf("Now searching string \"%s\" for characters \"%s\".\n", &source_string, &search_string);
  23.     printf("Now searching string \"%s\" for characters \"%s\".\n", source_string, search_string);
  24.  
  25.     puts("Cleaning up memory...");
  26.     free(source_string);
  27.     free(search_string);
  28.  
  29.     return EXIT_SUCCESS;
  30. }
  31.  
Raghuram
Feb 13 '08 #2
I can't believe I missed that. <shakes head>
It works perfectly now. Thank you for your help!
Feb 13 '08 #3

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

Similar topics

5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
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
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...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
12
by: whitehatmiracle | last post by:
Hi all Im not quite sure how to use the new and delete for a whole array dynamically. Actually i want that, a user inputs a char in a single char array. Everytime he inputs a char he creates a...
1
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...
14
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...
21
by: arnuld | last post by:
I have created a program to print the input words on stdout. Input is taken dynamically from stdin. In each word, each input character is allocated dynamically. I have ran this program with a file...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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 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.