Newbie question: Making arrays bigger 
Author Message
 Newbie question: Making arrays bigger

: I have a requirement for a table of names/phone numbers. Basically this is a
: struct of char name[80] and char telno[30], called dataLine. I have an array
: of 100 dataLines, which is a simple phone book.
:
: Question: the "100" is totally arbitrary. The number of entries could be 10,
: or 100,000. The data file can be reloaded while the program is running to
: update the database. How can I make my array bigger if, say, someone's added
: 100 phone numbers to the file? I don't want to specify a very big arbitrary
: maximum and hope for the best, because of the huge possible range. I'd much
: rather the program increased the array size to cope.

Instead of a fixed array

  struct dataLine table[100];

use a dynamic array, managed through malloc, realloc and free

  #include <stdlib.h>

  ...

  struct dataLine *table = 0;

  ...

  table = malloc( 100 * sizeof *table );
  if ( table == 0 ) {
    /* handle error */
  }

  ...

  /* table needs to be resized */
  table = remalloc( table, new_size );
  if ( table == 0 ) {
    /* handle error */
  }

  ...

  free( table );

: Please respond by e-mail. I will post a summary of responses later.

Post here, read here.

-- Mat.



Tue, 14 Aug 2001 03:00:00 GMT  
 Newbie question: Making arrays bigger

Quote:

> << Please respond by e-mail. >>

> Oh, rats! I had your answer all ready -- too bad. Please read the FAQ
> (http://www.eskimo.com/~scs/C-faq/top.html) to learn why you should not ask
> for an e-mail response.

Hi,

I read the FAQ list, but where does it talk about not asking for an e-mail
response?

okay to do so, provided that you post a summary (which Richard promised to
do).

Regards,

Arjan Bokx
--
(replace underscore with dash for email reply)
(vervang underscore door minstreepje voor antwoord per epost. Spam... grr)



Tue, 14 Aug 2001 03:00:00 GMT  
 Newbie question: Making arrays bigger


<snip>

Quote:

> Probably the victim of reading too fast.  Posting a summary is very rare.
> You see it about once a year.  Asking for 'free consulting' in the form
of
> email responses is a daily event.  So it appears P.L. was a bit quick on
the
> trigger.

I think we've had our quota for the year then. I recently posted a summary
of emails on the subject of the ANSIness or otherwise of the C/370 compiler
- a massive number of e-mails flooded into my mailbox and I posted
summaries of both of them. :-)

<snip>
--
Richard H

#include "sig.h"



Tue, 14 Aug 2001 03:00:00 GMT  
 Newbie question: Making arrays bigger
<< So it appears P.L. was a bit quick on the trigger. >>

So often the case. I will have to find the specific reference. I had always
assumed it was common sense, a dangerous assumption.

For now, here is the appropriate excerpt from the comp.lang.c++ FAQ at
http://www.cerfnet.com/~mpcline/c++-faq-lite/how-to-post.html#[5.4]

Key guidelines:

Do not say, "Please respond by e-mail because I don't normally read this
newsgroup". If you don't have enough time for the newsgroup, don't expect
the newsgroup to have enough time for you.
Do not post questions that are answered in the newsgroup's FAQ. That's like
saying your time (to read the FAQ) is more valuable than the time of
hundreds and hundreds of others (to answer your question). Tres uncool. Read
the FAQ first!
Do not cross post your question to a big pile of newsgroups. Post to the
newsgroup (singular) that best fits your question. If you don't get an
answer in the "right" newsgroup, post somewhere else but redirect followups
back to the "right" newsgroup.
Do include a working e-mail address in your signature. If your From: address
is not correct, please notify your system administrator. Until it's fixed,
add a Reply-to: line that gives your correct e-mail address.
--

Paul Lutus
www.arachnoid.com

<snip>



Tue, 14 Aug 2001 03:00:00 GMT  
 Newbie question: Making arrays bigger


Quote:
> << So it appears P.L. was a bit quick on the trigger. >>

> So often the case. I will have to find the specific reference. I had always
> assumed it was common sense, a dangerous assumption.

> For now, here is the appropriate excerpt from the comp.lang.c++ FAQ at
> http://www.cerfnet.com/~mpcline/c++-faq-lite/how-to-post.html#[5.4]

> Key guidelines:

> Do not say, "Please respond by e-mail because I don't normally read this
> newsgroup". If you don't have enough time for the newsgroup, don't expect
> the newsgroup to have enough time for you.
> Do not post questions that are answered in the newsgroup's FAQ. That's like
> saying your time (to read the FAQ) is more valuable than the time of
> hundreds and hundreds of others (to answer your question). Tres uncool. Read
> the FAQ first!
> Do not cross post your question to a big pile of newsgroups. Post to the
> newsgroup (singular) that best fits your question. If you don't get an
> answer in the "right" newsgroup, post somewhere else but redirect followups
> back to the "right" newsgroup.
> Do include a working e-mail address in your signature. If your From: address
> is not correct, please notify your system administrator. Until it's fixed,
> add a Reply-to: line that gives your correct e-mail address.

<Jack>

According to the FAQ, Paul, you are in serious violation!


and you have not added a Reply-to: line with a correct one.

Please notify your system administrator at once and flog yourself
senseless with a wet noodle!

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.



Wed, 15 Aug 2001 03:00:00 GMT  
 Newbie question: Making arrays bigger
Yes, I noticed this as I posted the guidelines (red-faced and hypocritical).
All I can say is, I really cannot simultaneously have a public e-mail
address and any semblance of a normal life in America. The reasons have to
do with matters of personal circumstance that are egregiously off-topic. I
do my best -- I post my Web site address (below), said site has a message
board by which I can be reached privately.
--

Paul Lutus
www.arachnoid.com


Quote:
><Jack>

>According to the FAQ, Paul, you are in serious violation!



Wed, 15 Aug 2001 03:00:00 GMT  
 Newbie question: Making arrays bigger

Quote:

> All I can say is, I really cannot simultaneously have a public e-mail
> address and any semblance of a normal life in America.

There is a way round this which is to use throw-away web based or
forwarding accounts, as I do. I know the hostility students get in

means that I don't get put into mailing lists and be stuck with them
permanently. I also change my Usenet address when it gets heavily
spammed or if I get bored with it. The system I'm using ATM of having a
web-based "from" address which deletes EVERYTHING and a forwarding
"reply-to" address which works and is unfiltered works well. I don't get
spammed and people who choose to reply to me don't have to demung my
address in order to do so.

Just my €0.02. . .

--
Marcus Houlden                                    ICQ: 29654055
http://www.deathsdoor.com/flimflam       PGP Key ID: 0x1A1C2EA3
Fingerprint: A784 D8E5 D7AC 012F 602A  2685 F6C9 D602 1A1C 2EA3



Thu, 16 Aug 2001 03:00:00 GMT  
 Newbie question: Making arrays bigger

Quote:

> Greetings,
> I have a requirement for a table of names/phone numbers. Basically this is a
> struct of char name[80] and char telno[30], called dataLine. I have an array
> of 100 dataLines, which is a simple phone book.

> Question: the "100" is totally arbitrary. The number of entries could be 10,
> or 100,000. The data file can be reloaded while the program is running to
> update the database. How can I make my array bigger if, say, someone's added
> 100 phone numbers to the file? I don't want to specify a very big arbitrary
> maximum and hope for the best, because of the huge possible range. I'd much
> rather the program increased the array size to cope.

Dynamic memory allocation of the memory as you need it.. Use realloc().
One way to store the data is in a text file with one record per line. You can
use a delimiter
character such as '|' to seperate the name from the phone number. As you read in
the data
from the test file, reallocate the needed memory. Here is an example in which
the data is
formated in the text file like this:

George Washington    |    (800) 222-3333
Abe Lincoln      |  222 333-4444

#include <stdio.h>
#include <stdlib.h>

typedef struct {
   char name[40];
   char nr[20];
   } DATA;

int main(void) {
   DATA *temp,*dataLine = NULL;
   char buf[80];
   unsigned i,record = 0;
   FILE *fp;

   if((fp = fopen("data.txt","r")) == NULL) {
      perror("Unable to open \"data.txt\"");
      exit(EXIT_FAILURE);
      }
   while(fgets(buf,sizeof buf,fp) != NULL) {
      if((temp = realloc(dataLine,(record + 1)*sizeof(*dataLine)))
               == NULL) {
         free(dataLine);
         puts("Unable to allocate memory");
         exit(EXIT_FAILURE);
         }
      dataLine = temp;
      if(sscanf(buf,"%40[^|] | %20[^\n]",dataLine[record].name,
                dataLine[record].nr) != 2) {
         free(dataLine);
         puts("File format error");
         exit(EXIT_FAILURE);
         }
      record++;
      }
   fclose(fp);
   printf("%u records in database\n\n",record);
   for(i = 0;i < record;i++)
      printf("  %-40s %s\n",dataLine[i].name,dataLine[i].nr);
   free(dataLine);
   return 0;
   }
--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Thu, 16 Aug 2001 03:00:00 GMT  
 Newbie question: Making arrays bigger

GMT in comp.lang.c.
Newbie question: Making arrays bigger's a cool scene! Dig it!

Quote:
>I have a requirement for a table of names/phone numbers. Basically this is a
>struct of char name[80] and char telno[30], called dataLine. I have an array
>of 100 dataLines, which is a simple phone book.

>Question: the "100" is totally arbitrary. The number of entries could be 10,
>or 100,000. The data file can be reloaded while the program is running to
>update the database. How can I make my array bigger if, say, someone's added
>100 phone numbers to the file? I don't want to specify a very big arbitrary
>maximum and hope for the best, because of the huge possible range. I'd much
>rather the program increased the array size to cope.

  The simple answer would be to use realloc() to re-allocate the array
every time it needs to change size. However, this may not be the most
efficient method, and is not usually what the professionals do.
  The most common method for doing this is to use a "linked list".
This is a structure that can store any number of items (within memory
limits, of course). Each item within a linked list, called a "node",
contains not only the all important data itself, but also a pointer to
the next node. A node of a double linked list also has a pointer to
the previous node. Each node is dynamically allocated as it is needed
and freed when it is no longer required.
  Linked lists are a rather advanced topic, though not really all that
hard a concept to understand in its basic form. If you want to know
more about this subject, I suggest you find a good book on data
structures and such.

Quote:
>Please respond by e-mail. I will post a summary of responses later.

  No.
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Fri, 17 Aug 2001 03:00:00 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Help making a really big array needed

2. Newbie question on making doors games / to other computers

3. Big Arrays arrays using B

4. Big Arrays arrays using Borland Turbo C...

5. Making a notepad that handles big files (64k).

6. Newbie Question: Arrays

7. Newbie Question: using delete with dynamic 2-d arrays

8. Newbie question about arrays

9. Newbie Question: Array of Points

10. Arrays newbie question

11. ArrayList to Array -- newbie question

12. newbie question: stings, pointers and arrays

 

 
Powered by phpBB® Forum Software