Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

dynamic arrays - store results

Posted on 2013-12-03
2
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

649 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