This code appears to work fine. That - in spite of ongoing STRANGE 'scanf()' behavior. (to which I seem to have found a 'fix').
I'm struggling with the function declaration:
void show_motorcycledat(struct motorcycles (*)[2]);
and use:
void show_motorcycledat(struct motorcycles (*pmc)[2])
Why not use both dimensions of the 2D array: "struct motorcycles (*)[3][2]" ?
Further, I thought I'd be using "pmc[row][col]->" for member access rather than "pmc[row][col]." as seen in function body. Thanks.
Code:
#include <stdio.h>
#define MAXROW 3
#define MAXCOL 2
#define SLENGTH 80
enum {RET_ERR = -1, RET_OK};
struct motorcycles {
char name[25];
int size;
float weight;
};
char G_inst[SLENGTH];
void show_motorcycledat(struct motorcycles (*)[2]);
int main(void) {
int c; // VOODOO 'scanf()' fix
int entrycount;
int row;
int col;
char letrs[MAXROW][MAXCOL];
int nums[MAXROW][MAXCOL];
struct motorcycles mc[MAXROW][MAXCOL];
printf("\nPlease enter %d lines.\n", MAXROW * MAXCOL);
printf("Each line: LETTER <sp> NUMBER <sp> Motorcycle length <sp> MC weight <sp> MC name\n\n");
entrycount = 0;
for (row = 0; row < MAXROW; row++) {
for (col = 0; col < MAXCOL; col++) {
printf("Entry %d: ", ++entrycount);
scanf("%c %d %d %f %[^\n]", &letrs[row][col], &nums[row][col],
&mc[row][col].size, &mc[row][col].weight, mc[row][col].name);
while ((c = getchar()) != '\n' && c != EOF ); // VOODOO code for 'scanf()' fix
}
}
entrycount = 0;
printf("\nResults:\n\n");
for (row = 0; row < MAXROW; row++) {
for (col = 0; col < MAXCOL; col++) {
printf("%d: (R: %d . C: %d) %c %d %d %.2f %s\n", ++entrycount, row, col, letrs[row][col], nums[row][col],
mc[row][col].size, mc[row][col].weight, mc[row][col].name);
}
}
show_motorcycledat(mc);
printf("\n");
return(RET_OK);
}
void show_motorcycledat(struct motorcycles (*pmc)[2]) {
int row;
int col;
printf("\nResults using struct *:\n\n");
for (row = 0; row < MAXROW; row++) {
for (col = 0; col < MAXCOL; col++) {
printf("(R: %d . C: %d) %d %.2f %s\n", row, col, pmc[row][col].size, pmc[row][col].weight, pmc[row][col].name);
}
}
}