M
Matt Kowalczyk
Hello,
Can someone explain to me why the following code works correctly? It would seem
like it shouldn't.
irc_client.c contains:
#include "ircclient.h
IRCClient* init_irc_client() {
IRCClient* irc_client;
irc_client = malloc(sizeof(IRCClient));
irc_client->irc_connect_to = irc_connect_to;
// return irc_client;
}
irc_client.h contains:
typedef struct {
/* methods */
int (*irc_connect_to) (xdcc_packet* packet);
} IRCClient;
typedef struct {
char* server;
int port;
char* channel;
char* nick;
int packet_nr;
} xdcc_packet;
My main.c contains:
#include <assert.h>
#include "ircclient.h"
int main(int argc, char* argv[]) {
IRCClient* client;
xdcc_packet packet;
/* packet init code goes here - deleted for clarity */
client = init_irc_client();
assert(client);
client->irc_connect_to(&packet);
free_irc_client(client);
free_xdcc_packet(&packet);
return 0;
}
My question is, init_irc_client() initializes client in the main function
regardless of whether init_irc_client() contains the return statement or not. In
my example code above, I have the return statement commented out, however when I
call client->irc_connect_to, the appropriate function is called. Why is client
getting initialized regardless of the fact that init_irc_client returns a
pointer to a client or not.
The assert(client) statement _only_ fails if I don't make the assignment:
client = init_irc_client();
Thanks,
Matt
Can someone explain to me why the following code works correctly? It would seem
like it shouldn't.
irc_client.c contains:
#include "ircclient.h
IRCClient* init_irc_client() {
IRCClient* irc_client;
irc_client = malloc(sizeof(IRCClient));
irc_client->irc_connect_to = irc_connect_to;
// return irc_client;
}
irc_client.h contains:
typedef struct {
/* methods */
int (*irc_connect_to) (xdcc_packet* packet);
} IRCClient;
typedef struct {
char* server;
int port;
char* channel;
char* nick;
int packet_nr;
} xdcc_packet;
My main.c contains:
#include <assert.h>
#include "ircclient.h"
int main(int argc, char* argv[]) {
IRCClient* client;
xdcc_packet packet;
/* packet init code goes here - deleted for clarity */
client = init_irc_client();
assert(client);
client->irc_connect_to(&packet);
free_irc_client(client);
free_xdcc_packet(&packet);
return 0;
}
My question is, init_irc_client() initializes client in the main function
regardless of whether init_irc_client() contains the return statement or not. In
my example code above, I have the return statement commented out, however when I
call client->irc_connect_to, the appropriate function is called. Why is client
getting initialized regardless of the fact that init_irc_client returns a
pointer to a client or not.
The assert(client) statement _only_ fails if I don't make the assignment:
client = init_irc_client();
Thanks,
Matt