473,396 Members | 1,833 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,396 software developers and data experts.

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

1
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 1166
weaknessforcats
9,208 Expert Mod 8TB
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
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++)...
2
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 =...
13
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. ...
10
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 =...
2
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...
1
by: aurora | last post by:
I something like this: dim mybytes() as byte = "hello world" The string is ascii.
3
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...
12
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
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...
0
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
0
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...
0
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,...

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.