Hi iismatt,
Try the following:
typedef struct{
char myString[99];
} myStruct;
myStruct arrayOfStructs[10];
David Maisonave :-)
Cheers!
Main Topics
Browse All TopicsGuys (and girls),
Two questions. Basically, I want to design an array of structs in C. And in these structs I want dynamic string allocation. I'm having major problems implementing this in the C language. Can anyone tell me where I'm going wrong?
My code would look something like this...
Struct {
char myString[];
} myStruct;
myStruct arrayOfStructs[10];
Without even taking the dynamic strings within the structs into account, I can't get my program to recognize myStruct as a valid type. What am I missing?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>>>
You can't have an array of indeterminate size in a structure. The usual approach would be to put a pointer in the structure and set the pointer to point to some dynamically allocated memory.
>>>
char[] myString; is syntactically equivalent to char* myString;
the s in "struct" must be lowercase. Axter's post has the syntax you're looking for. :)
If you want to use your approach of char[] myString, you're going to need to allocate memory individually for each pointer using malloc/calloc.
The keyword struct needs to be lowercase.
You can either do a typedef as in my above example, or you need to use the keyword struct when you declare a variable of that type.
Example:
struct myStruct2{
char myString[99];
};
struct myStruct2 arrayOfStructs2[10];
You could also declare a single instance of struct via following method:
struct{
char myString[99];
} myStruct3;
The above is a single instance of a struct which declares the type, and the variable name for it (myStruct3).
Example usage with pointer, as previous experts have suggested.
#include <memory.h>
typedef struct{
char *myString;
int string_size;
} myStruct;
myStruct arrayOfStructs[10];
int main(int argc, char* argv[])
{
int i;
for(i = 0;i < sizeof(arrayOfStructs) / sizeof(arrayOfStructs[0]);
{
arrayOfStructs[i].myString
arrayOfStructs[i].string_s
memset(arrayOfStructs[i].m
}
>>char myString[]; is an invalid declaration in C, but for some reason if you put that inside of a struct declaration, it compiles; in that case I think char myString[] IS
>>equivalent to char* myString; but I'm not 100% positive.
That depends on your compiler. It doesn't compile on MS VC++ 6.0, but it does compile on GNU 3.x and Bordland 5.5.1.
Business Accounts
Answer for Membership
by: efnPosted on 2005-04-10 at 19:34:03ID: 13750029
You can't have an array of indeterminate size in a structure. The usual approach would be to put a pointer in the structure and set the pointer to point to some dynamically allocated memory.