G
Grant Austin
What would be the correct syntax for setting up
a dynamic array of structs?
Suppose you have a struct declared:
struct relation {
FILE * binFile;
unsigned int numAttrs;
struct attrList * relAttrs; /* definition shown at end of post */
};
typedef struct relation relHeader;
The array only has to be sized once. The size is read from a text
file.
Currently I have something like this:
#include <stdio.h>
int main(void){
unsigned int relCount = 2;
relHeader Headers[relCount-1];
return 0;
}
I would prefer to be able to define Headers as:
relHeader * Headers;
And then malloc space for whatever size I need.
When I try:
Headers = malloc(sizeof(*Headers) * (relCount-1))
I get a warning and a syntax error as follows:
p3.c:26: warning: assignment makes pointer from integer without a cast
p3.c:26: syntax error before "Headers"
What I have will work well enough for my purposes. If it's something
simple I'm missing I'd like to fix it.
Regards,
Grant
struct attribute {
struct attribute * next;
unsigned int attrLen;
char type;
char * attrName;
};
typedef struct attribute attrList;
a dynamic array of structs?
Suppose you have a struct declared:
struct relation {
FILE * binFile;
unsigned int numAttrs;
struct attrList * relAttrs; /* definition shown at end of post */
};
typedef struct relation relHeader;
The array only has to be sized once. The size is read from a text
file.
Currently I have something like this:
#include <stdio.h>
int main(void){
unsigned int relCount = 2;
relHeader Headers[relCount-1];
return 0;
}
I would prefer to be able to define Headers as:
relHeader * Headers;
And then malloc space for whatever size I need.
When I try:
Headers = malloc(sizeof(*Headers) * (relCount-1))
I get a warning and a syntax error as follows:
p3.c:26: warning: assignment makes pointer from integer without a cast
p3.c:26: syntax error before "Headers"
What I have will work well enough for my purposes. If it's something
simple I'm missing I'd like to fix it.
Regards,
Grant
struct attribute {
struct attribute * next;
unsigned int attrLen;
char type;
char * attrName;
};
typedef struct attribute attrList;