gcc 4.4.4 c89
J'ai toujours fait ce qui suit lors de l'utilisation de structures pour cacher les éléments dans le fichier d'implémentation.révision de 2 méthodes différentes de typedef'ing une structure
fichier d'en-tête de port.h
struct port_tag;
struct port_tag* open_ports(size_t port_id);
void close_ports(struct port_tag *port);
fichier d'implémentation de port.c
#include "port.h"
typedef struct port_tag {
size_t port_id;
} port_t;
port_t* open_ports(size_t port_id)
{
port_t *port = malloc(sizeof *port);
if(port == NULL) {
return NULL;
}
port->port_id = port_id;
return port;
}
void close_ports(port_t *port)
{
if(port != NULL) {
free(port);
}
}
fichier pilote driver.c
#include "port.h"
int main(void)
{
size_t i = 0;
struct port_tag *port = NULL;
for(i = 0; i < 5; i++) {
port = open_ports(i);
if(port == NULL) {
fprintf(stderr, "Port [ %d ] failed to open\n", i);
}
close_ports(port);
}
return 0;
}
Dans le code ci-dessus, il est clair que la balise le nom est port_tag et le nom réel de type typedef'ed est port_t.
Cependant, je suis en train de réorganiser un code. Et j'ai trouvé qu'ils ont utilisé une méthode différente, que je n'avais jamais vue auparavant. J'ai quelques questions sur leur méthode.
fichier d'en-tête de channel.h
typedef struct channel_t channel_t;
channel_t* open_channels(size_t channel_id);
void close_channels(channel_t *channel);
fichier d'implémentation de channel.c
#include "channel.h"
struct channel_t {
size_t channel_id;
};
channel_t* open_channels(size_t channel_id)
{
channel_t *channel = malloc(sizeof *channel);
if(channel == NULL) {
return NULL;
}
channel->channel_id = channel_id;
return channel;
}
void close_channels(channel_t *channel)
{
if(channel != NULL) {
free(channel);
}
}
fichier pilote driver.c
#include "channel.h"
int main(void)
{
size_t i = 0;
channel_t *channel = NULL;
for(i = 0; i < 5; i++) {
channel = open_channels(i);
if(channel == NULL) {
fprintf(stderr, "Channel [ %zu ] failed to open\n", i);
}
close_channels(channel);
}
return 0;
}
1) Quand ils ont déclaré typedef struct qui est le nom de l'étiquette ou le nom de la struct lui-même?
typedef struct channel_t channel_t;
2) Dans le fichier d'implémentation, le nom de la structure ne doit-il pas suivre la dernière accolade?
struct channel_t <-- tag name {
size_t channel_id;
} <-- itsn't this the name of the typedef'ed struct;
Un grand merci pour tous les conseils,
+1 Pour une question intéressante et méticuleusement présentée. J'ai appris quelque chose car j'expose toujours l'implémentation de la structure au code client, inutilement comme il s'avère. –