Hello All:

I want to create an array of structures like so:

#define MAX 10
struct hash_tbl {
int key;
int data;
};
struct hash_tbl hash_array[MAX],  *ptr;

Now, I am having some issues in attempting to "loop" through my
array of structures and just generate the some data for the fields:

for (i=0; i<=MAX_ADDR; i++) {

             sptr->key[i]  = (i * 100 + 13579);
             sptr->data[i] = (i * 100);
        }
printf("HASH KEY:==> %d DATA:==> %d\n", sptr[k]->key,
                                                        sptr[k]->data);

Results in:

keh_hash.c:31: error: subscripted value is neither array nor pointer
keh_hash.c:32: error: subscripted value is neither array nor pointer
keh_hash.c:41: error: invalid type argument of `->'
keh_hash.c:42: error: invalid type argument of `->'

So, could someone please assist on how I would walk through the creation of my array and how to print it out.

Thanks so much....!!!!!!!!!!!!

what's a sptr

sptr->key = (i * 100 + 13579);

You don't have anything defined as a sptr...ptr yes but not sptr..

Plus your treating key like an array by using the [] but you have key defined as a structure member


struct hash_tbl {
int key;/*note not an array*/
int data;
};

Oh Sorry about that...! It should be just "ptr". And the array of
structures are built on line six(6).

struct hash_tbl hash_addr[MAX]

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.