Solved

An Array of structs

Posted on 2005-04-10
16,791 Views
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
Question by:iismatt

LVL 15

Expert Comment

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

Hi iismatt,
Try the following:
typedef struct{
char myString[99];
} myStruct;

myStruct arrayOfStructs[10];

David Maisonave :-)
Cheers!
0

LVL 1

Expert Comment

>>>
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

LVL 30

Expert Comment

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

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

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

>>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

OK.  This is great.  I got it.  Thanks guys.
0

Featured Post

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 and use nested-loops in the C programming language.