Skip to main content
added 421 characters in body
Source Link
chux
  • 638
  • 4
  • 13

Additional design idea for #3

When possible, also provide the maximum size needed for mytype in the same .h file as mytype_to_string().

#define MYTYPE_TO_STRING_SIZE 256

Now the user can code accordingly.

char buf[MYTYPE_TO_STRING_SIZE];
puts(mytype_to_string(mt, buf, sizeof buf));

Order

The size of arrays, when first, allows for VLA types.

char * mytype_to_string( const mytype_t *mt, size_t bufsize, char *buf[bufsize]); 

Not so important with single dimension, yet useful with 2 or more.

void matrix(size_t row, size_t col, double matrix[row][col]);

I recall reading having the size first is a preferred idiom in the next C. Need to find that reference....

Additional design idea for #3

When possible, also provide the maximum size needed for mytype in the same .h file as mytype_to_string().

#define MYTYPE_TO_STRING_SIZE 256

Now the user can code accordingly.

char buf[MYTYPE_TO_STRING_SIZE];
puts(mytype_to_string(mt, buf, sizeof buf));

Additional design idea for #3

When possible, also provide the maximum size needed for mytype in the same .h file as mytype_to_string().

#define MYTYPE_TO_STRING_SIZE 256

Now the user can code accordingly.

char buf[MYTYPE_TO_STRING_SIZE];
puts(mytype_to_string(mt, buf, sizeof buf));

Order

The size of arrays, when first, allows for VLA types.

char * mytype_to_string( const mytype_t *mt, size_t bufsize, char *buf[bufsize]); 

Not so important with single dimension, yet useful with 2 or more.

void matrix(size_t row, size_t col, double matrix[row][col]);

I recall reading having the size first is a preferred idiom in the next C. Need to find that reference....

Source Link
chux
  • 638
  • 4
  • 13

Additional design idea for #3

When possible, also provide the maximum size needed for mytype in the same .h file as mytype_to_string().

#define MYTYPE_TO_STRING_SIZE 256

Now the user can code accordingly.

char buf[MYTYPE_TO_STRING_SIZE];
puts(mytype_to_string(mt, buf, sizeof buf));