Solved

dynamic array of static sized strings

Posted on 2002-05-31
7
196 Views
Last Modified: 2010-04-15
I want to create a dynamic array of strings, each string of size 100.  I know I can use char **ppMyArray, allocate the number I want, and then allocate each pointer to size 100.  My question: Is there a way to create the array without the need to loop through and allocate each pointer to size 100?
0
Comment
Question by:marvinm
  • 3
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 200 total points
Comment Utility
Sure. If you declare the array:

char (* dynarray)[100];

Then you can allocate as many elements as you want with:

dynarray=calloc(numberofelements,100*sizeof(char));
0
 
LVL 1

Author Comment

by:marvinm
Comment Utility
how can I do the allocation to avoid
warning: assignment from incompatible pointer type?
Thanks!
0
 
LVL 6

Expert Comment

by:Triskelion
Comment Utility
Can you put the string in a structure and dynamically create them?

typedef struct tagSTRING_THING
{
     char     strData[100];
} STRING_THING;
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:marvinm
Comment Utility
a = (char (*)[100])malloc() seems to work
If I declare a char **, then I need to malloc num_elements * sizeof (char *), and then for each one num_elements*100
do I need to allocate num_elements * ((sizeof(char *)+100)?
Thank You
(this is the last part of my question)
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
Almost. The declaration:

char (*a)[100];

is a pointer to an array of 100 characters. So what you are allocating is multiples of arrays of 100 characters. So the malloc would be:

a=(char (*)[100])malloc(numelements*sizeof(char)*100);

For calloc it would be:

a=(char (*)[100])calloc(numelements,sizeof(char)*100);



This is, for completeness, in contrast to:

char **a;

With this declaration a is a pointer to a pointer. Semantically you wind up treating them the same. But the memory layout is different. In the first case you have a single pointer to a block of memory that represents some number of 100 byte arrays. In the second case you have a pointer to an array (of some size) of pointers. Each of the elements in the "second" array then points to individual arrays of 100 bytes. So in the first case a location is found with a pointer dereference, followed by a 2 calculations (find the block, then calculate where the desired array is, then calculate the position of the element). In the second case there are two dereferences followed and two calculation (find the second array, calculate which pointer you want, dereference that, then calculate where the element is).
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
P.S. note that the difference in the memory layout at the root of why they have to be malloced differently. In the first case there is a single block of memory, so a single malloc or calloc can fill the requirements.

But in the second case there are separate memory blocks for each array, plus one for the pointer array. Therefore you have to do all the mallocs or callocs separately.
0
 
LVL 1

Author Comment

by:marvinm
Comment Utility
Great!
Thank You
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

772 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

15 Experts available now in Live!

Get 1:1 Help Now