473,699 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I convert this string array to an array of dynamic strings in C

1 New Member
I need to read a txt and store it in a dynamic string array
the code I have now does but memory is wasted because it is static

I like to read a txt and every word stored in a string array of dynamic


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.      #include <conio.h>
  3. #define MAX_CHARS 20
  4.  
  5. typedef char string[MAX_CHARS+1];  // leave one space for '\0'
  6.  
  7. main(){
  8.    int i;
  9.    string array[4];
  10.  
  11.    FILE *data;
  12.    data = fopen("ard.txt","r");
  13.    for(i = 0; i < 4; i++)
  14.         { fscanf(data, "%s", array[i]); } // no need for & with %s
  15.            printf("%s",array[3]);
  16.    getch();
  17. }
  18.  
  19.  
Aug 31 '16 #1
1 1180
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your static array is:

Expand|Select|Wrap|Line Numbers
  1. string array[4];
My first question is: How do you know memory is wasted? I mean, how long are the strings in the elements of this array. If you had 4 strings of 2000 chars each or if you had 4 strings of 10 chars each, your array would be coded the same way.

You would have to look at the implementation of the string template to see if memory is wasted. These are C++ string objects and not some char array in C with a \0 terminator.
Sep 1 '16 #2

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

Similar topics

2
5428
by: raxitsheth | last post by:
I am using array in my progrm... is there any tool /tips so that i can convert my program to Dynamic Array...so that I can Add more Element to Array.... Code is around 4000 line 'C' (not C++) program....
2
3655
by: andrea_ | last post by:
I've got a problem: I must use an int java array in a C code. Using JNI i wrote this code: jsize len = (*env)->GetArrayLength(env, payload); //payload is the name of java array jint *body = (*env)->GetIntArrayElements(env, payload, 0); int cpayload ; // C array int i; for (i=0; i<len; i++) {
13
6115
by: Dan V. | last post by:
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93? I am trying to create a one line text file with these characters all one one row with no spaces. 1. 144 = 0x90 2. 147 = 0x93 3. STX = (^B = 2 = 0x2) 4. NUL = (^@ = 0 = 0x0)
10
5272
by: Eranga | last post by:
I have the following code; string test1 = words;//"Exclud" string test2 ="Exclud"; string test3 = String.Copy(words);//"Exclud" bool booTest1 = test1.Equals(test2);//false bool booTest2 = test2.Equals("Exclud");//true bool booTest3 = test1.Equals("Exclud");//false bool booTest4 = words.Equals("Exclud");//false bool booTest5 = test3.Equals("Exclud");//false
2
1361
by: Ivan Weiss | last post by:
Okay here is one I cannot figure out. I need to declare an array to hold the values for a row in my database that I am going to populate a listview control with. However, the code is in a class so I can re-use it for any table in my database so the array length is variable at run time. How can I declare a variable like this? Currently my code is the following:
1
1555
by: aurora | last post by:
I something like this: dim mybytes() as byte = "hello world" The string is ascii.
3
4329
by: XML newbie: Urgent pls help! | last post by:
Does anyone have a snippet of code that will convert a string array to a long array? I've nearly smashed my head against the wall trying to figure this out. I'm Using vb.net 2005 Pls reply asap. I thaanku all in advance. God bless u. Pls reply with code.
12
43511
by: JackYee123 | last post by:
Hey, I need a structure to store a string array in c, for example Index Content -------- ----------- 0 word1 1 word2 2 3
9
3890
by: =?Utf-8?B?U0FM?= | last post by:
Is it possible to Cast an Object to a String Array the way I'm doing it in the code below? I'm trying to reuse code I have without creating new code if I can. Dim asNames() As String Dim asValues() As String Dim sKeyPairValues As String = String.Empty Dim oNames Object Dim oValues Object
0
1322
by: =?utf-8?q?C=C3=A9dric_Lucantis?= | last post by:
Le Thursday 19 June 2008 17:12:08 Jonno, vous avez écrit : string = '0.906366 2.276152 0.013369 80.773141' array = -- Cédric Lucantis
0
8705
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
8628
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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...
1
8943
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7785
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...
1
6550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5884
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3075
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
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.