?
Solved

An Array of structs

Posted on 2005-04-10
8
Medium Priority
?
16,812 Views
Last Modified: 2011-08-18
Guys (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?
0
Comment
Question by:iismatt
8 Comments
 
LVL 15

Expert Comment

by:efn
ID: 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.
0
 
LVL 30

Accepted Solution

by:
Axter earned 1000 total points
ID: 13750040
Hi iismatt,
Try the following:
typedef struct{
    char myString[99];
} myStruct;

myStruct arrayOfStructs[10];


David Maisonave :-)
Cheers!
0
 
LVL 1

Expert Comment

by:VolatileVoid
ID: 13750046
>>>
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.
0
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.

 
LVL 30

Expert Comment

by:Axter
ID: 13750051
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).
0
 
LVL 1

Expert Comment

by:VolatileVoid
ID: 13750055
Ah, wait, my apologies to efn.
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13750069
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]);++i)
      {
            arrayOfStructs[i].myString = malloc(123);
            arrayOfStructs[i].string_size = 123;
            memset(arrayOfStructs[i].myString, 0, arrayOfStructs[i].string_size);
      }
0
 
LVL 30

Expert Comment

by:Axter
ID: 13750082
>>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.
0
 
LVL 1

Author Comment

by:iismatt
ID: 13750120
OK.  This is great.  I got it.  Thanks guys.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 how to use strings and some functions related to them in the C programming language.
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.

807 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