iismatt
asked on
An Array of structs
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?
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?
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>>
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.
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).
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).
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.
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.
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_s ize = 123;
memset(arrayOfStructs[i].m yString, 0, arrayOfStructs[i].string_s ize);
}
#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.
>>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.
ASKER
OK. This is great. I got it. Thanks guys.