Solved

dynamic arrays - store results

Posted on 2013-12-03
2
183 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now