You may like this trick. :) If you treat your records as strings then you can easily place them in a single array and work with them using standard string operations. The cost is two extra characters per element.
#include <stdio.h>
#include <string.h>
#define MAX_ELEMENTS 2
char table[MAX_ELEMENTS][20];
void insert_record ( const char *record, int row )
{
memcpy ( table[row], record, 12 );
memcpy ( table[row] + 13, record + 12, 6 );
}
void print_record ( int row )
{
printf ( "%s\n", table[row] );
printf ( "%s\n", table[row] + 13 );
}
int main ( void )
{
insert_record ( "abcdefghijkl123456", 0 );
insert_record ( "lkjihgfedcba654321", 1 );
print_record ( 0 );
print_record ( 1 );
} If the cost is too much for you then you can use fixed widths and forget about strings with only minor changes:
#include <stdio.h>
#include <string.h>
#define MAX_ELEMENTS 2
char table[MAX_ELEMENTS][18];
void insert_record ( const char *record, int row )
{
memcpy ( table[row], record, 12 );
memcpy ( table[row] + 12, record + 12, 6 );
}
void print_record ( int row )
{
printf ( "%.*s\n", 12, table[row] );
printf ( "%.*s\n", 6, table[row] + 12 );
}
int main ( void )
{
insert_record ( "abcdefghijkl123456", 0 );
insert_record ( "lkjihgfedcba654321", 1 );
print_record ( 0 );
print_record ( 1 );
} Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401