Hi, I'm creating another random program and I found a problem. I need a way to kind of create an array of structs. Like this, I need 'collection[0]' to represent a struct with a char array and an int equal to the struct number, in this case 0. And I need the 'collection' array(or whatever you'd call it) to hold a massive amount of these structs. I really can't explain it better than so. I get the feeling I'm missing something incredibly obvious. I'm sure a google search would have great result but I simply don't know what I'm really looking for here.
Any ideas of what I'm missing here?Live the life as you know it
/ Wille
i'm not knowledgable of C, (trying to learn), but to me it looks like you just need a big 2-D array of type Char (or string). then reference each dimension ("line") by the integer. If you really have to have the integer stored in the array, just make the first n char.s as parts of the integer. f/e: to make
a 16-bit number: x=asc(strA(z,1)); y=asc(strA(z,2)); numbr=x*(2^8)+y
for larger number, keep increasing powers of two by multiples of 8. then tell your program to skip or delete the initial char.s (the number).
you could also store the length of each line at the beginning of each line, which would, with calculation, yield the start-position of the next "line", thus making the 2-d array into a
1-d array with indexes, but why re-invent the wheel? that's what the storage cross-ref does anyway.
(then again, i might be headed for alpha-centauri when i'm supposed to be landing on the moon... in other words, lost without a clue... and running out of air)
I'm not sure if it's what you're after, but *maybe* something like below will work... that is, if it's valid C. :P No major bounds / error checking of any kind.
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXREC 5 #define MAXSTR 21 typedef struct { char s[MAXSTR]; int num; } structA; typedef struct { structA *collection; int cur; } structB; void setRecord(structB *tmp) { char *p; printf("Enter a string of no more than %d chars: ", MAXSTR - 1); fgets(tmp->collection[tmp->cur].s, sizeof tmp->collection[tmp->cur].s, stdin); if((p = strchr(tmp->collection[tmp->cur].s, '\n')) != NULL) { *p = '\0'; } printf("Enter a value for \"num\": "); scanf("%d", &tmp->collection[tmp->cur].num); } int main(void) { structB test = { NULL, 0 }; int i; test.collection = (structA *)malloc(MAXREC * sizeof(*test.collection)); if(test.collection != NULL) { for(i=0; i < MAXREC; i++) { strcpy(test.collection[i].s, "empty"); test.collection[i].num = 0; } } else { puts("Allocation error"); return (1); } printf("Enter record (0-%d): ", MAXREC - 1); scanf("%d%*c", &test.cur); setRecord(&test); for(i=0; i < MAXREC; i++) { test.cur = i; printf("record (%d)\ns=%s\nnum=%d\n\n", test.cur, test.collection[test.cur].s, test.collection[test.cur].num); } free(test.collection); return (0); }HTH
Yeah, it was simpler than I thought. Thanks guys! Live the life as you know it
/ Wille
Edited: deleted whole reply. Had an idea but as soon as I posted it, I realised I was wrong. Pity there isn't a [delete message] button, only an [edit message] button.
Try looking at the vector and list classes from the STL, especially if you're using a lot of items.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |