Tim Rentsch wrote:
Noob wrote:
Tim Rentsch wrote:
Noob wrote:
Consider the following code defining an array type:
typedef unsigned char toto[16];
int foo(toto *p1);
int bar(unsigned char *p2);
Some unsolicted advice:
Au contraire, that's what I was looking for!
As a general rule don't use typedef names for array types. The
reason is arrays in C are just different from all other object
types, and using a typedef name for an array is likely to be
confusing.
I'll explain why I did it. To be blunt, A few people on my team
are not very good at C. If my API functions just had "pointer to
unsigned char" parameters, I'd have all sorts of whacky misuses
of the API. [snip examples]
So I wanted to declare an opaque type that could be allocated
with auto storage duration. An array typedef /seemed/ like a
good idea at the time...
I see.. the problem you're trying to solve is basically not a
technical problem but a cultural problem (in the sense of
"engineering culture").
I suppose I could wrap the array in a struct [which is then
typedef'ed to 'toto'] and then I'd have
int foo(toto *p1);
int bar(unsigned char *p2)
but once again, I'd have to cast or copy when bar calls foo.
I'm unclear on how functions like 'bar()' fit into the picture.
Can you explain how they are used and what role they play? Are
they really necessary in the overall design or might they be
provided in some other form?
I'll try and provide some background.
Originally, the entry point to my library was foo, and foo dealt
with 128-bit AES keys, so I had:
typedef uint8_t AESkey[16];
int foo(int op, void *buf, int buflen, AESkey *key);
Later, I had to integrate a third-party library (TPL) that needs
to call foo (nit: it doesn't call foo directly, I have a wrapper
that does some pre- and post- processing). TPL uses "traditional"
pointers as parameters for keys.
So basically, I have defined this wrapper:
int wrap(uint8_t *TPL_key);
and the wrapper is supposed to call foo to get the job done.