Solved

dynamic arrays - store results

Posted on 2013-12-03
2
187 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:
Kent Olsen 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 206
Using unmanaged DLL from managed application 14 294
Grammars for C C++ and java 1 131
Setting nameservers after res_init fails doing res_query 2 109
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 recursion in the C programming language.

829 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