Simple cross array of sizes

I recently coded this in less than 30 seconds for a student showing how you can create a simple 2D array to return sizes of structs to be used inside of buffer array function. This should be explanatory by itself... the use - its up to you.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct type1_s {
    int data;
}type1_t;

typedef struct type2_s {
    int data;
    int data2;
}type2_t;

#define TYPE_1 10
#define TYPE_2 20
#define TYPE_1_SZ sizeof(type1_t)
#define TYPE_2_SZ sizeof(type2_t)

int typesize[] = {TYPE_1_SZ, TYPE_2_SZ};
int type[] = {TYPE_1, TYPE_2};

int main(int argc, char** argv) {

    int i = 0;
    for (i = 0; i < (sizeof(type)/sizeof(int)); i++) {

        printf("%i. type: %d size: %d\n", i,  type[i], typesize[i]);
    }

    return(0);
}

Blog tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <python> <c>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.