Solved

dynamic arrays - store results

Posted on 2013-12-03
2
186 Views
Last Modified: 2013-12-08
Hi,

Can anyone help me how to store results in dynamic arrays, I mean I am not willing to fix array size. I have to store many values in array. For example, lets say I am storing 100 client names and other time I can store 10000 names so it should be dynamic, I cannot fix size.

I need help for how to create it, use it/ call .

Please advise and help.

Thanks
0
Comment
Question by:Johny12345
2 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 39693555
Hi Johny,

If you're going to keep an array of strings, I suggest that you just keep an array of pointers to strings.  It codes a bit easier and can take up less memory.

Object oriented programming makes this a bit easier as it encapsulates all of the controls into one place.  You can do it in C, but if you want to manage multiple dynamic arrays, you probably want to do that with a control structure.  Let's assume just 1 array for now.

//  Define the basic variables.

  char **StringArray = NULL;
  int  StringArrayLength = 0;
  int  StringArrayPosition = 0;

//  Use a function to add items.  (It's just cleaner this way.)

char *AddName (char *Name)
{
  char *N;
  if (StringArrayPosition == StringArrayLength)  // no room to add an item
  {
    StringArrayLength +=50;  // Add 50 pointers every time that it grows
    StringArray = (char**) realloc (StringArray, StringArrayLength * sizeof (char*));
  }
  N = strdup (Name);
  StringArray[StringArrayPosition++] = N;
  return (N);
}

Open in new window


There are a lot of competing theories concerning how to grow that array.  Growing by 1 keeps the array the smallest, but the overhead is significant as you copy and grow the array with every add.  Purists will create it with a default length, then double the size every time it grows.  All are effective so it's just personal preference.

Anyway, that should get you started.


Good Luck!
Kent
0
 

Author Closing Comment

by:Johny12345
ID: 39705121
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question