Click here for important information about

C Collection of items

Lenovo / 42334dg
January 3, 2010 at 19:17:54
Specs: Windows 7, 2 GHz DualCore/ 2041 MB
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

See More: C Collection of items

January 3, 2010 at 20:10:06
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)

Report •

January 3, 2010 at 21:18:30
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);   


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

      test.cur = i; 

      printf("record (%d)\ns=%s\nnum=%d\n\n", test.cur, 


   return (0);


Report •

January 4, 2010 at 13:22:29
Yeah, it was simpler than I thought. Thanks guys!

Live the life as you know it
/ Wille

Report •

Related Solutions

January 4, 2010 at 14:50:45
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.

Report •

January 4, 2010 at 19:18:18
Try looking at the vector and list classes from the STL, especially if you're using a lot of items.

Report •

Ask Question