-
Pointers and structures
I have a problem when I try to allocate some memory for my structrure.
Code:
#define BUFSIZE 100
typedef struct hash_t{
char *buffer[50];
unsigned int *t_index;
}HASH_T;
HASH_T *phtable[BUFSIZE];
if ((phtable = ((struct hash_t *) malloc(sizeof (struct hash_t)*100))) == NULL)
printf("Allocation error");
At compilation I get:"Operands of = have incompatible types 'struct hash_t * [100]' and 'struct hash_t *'." " Lvalue required".
Thank you
-
You're taking an array and trying to assign it something. If you want to dynamically allocate a block of memory and treat it like an array, then you use a pointer.
-
Hi,
You have declared a two dimensional array of structures:
HASH_T *phtable[BUFSIZE];
i,e; pointer phtable points to the first element of BUFSIZE no. of structures of type hash_t...
so you ned to allocate memory for each element..
since phtable is a pointer to the two dimensional array...it needs the array index([]),
thats why the compiler is throwing the error : " Lvalue required".
try this out
Code:
for( count = 0 ; count < BUFSIZE ; ++count) {
if ((phtable[count] = ((struct hash_t *) malloc(sizeof (struct hash_t)*1))) == NULL)
printf("Allocation error");
}
This might resolve the problem...
-
you do not need to cast malloc in C - see FAQ