LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2003, 05:25 AM   #1
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
Post using struct type X as pointer in struct X.


Hello.

Can I "predeclerate" a struct type so I can use it in the struct itself ?

Example:

<code>

typedef struct {
void* myData;
myDataList* next;
myDataList* prev;
} myDataList;

</code>

The abow is an example, this is realy what I want to do:

<code>

void ImAnCallBack( myCBConfig* data, char* rawTcpData );

typedef struct {
char* cmd; // Command to match
char* data; // Additional data to callback
void (void* callback)( myCBConfig*, char* rawTcpData); // Callback to call
} myCBConfig;

</code>

I will create a list of myCBConfig. When I get a command on TCP (text) I will match it against myCBConfig.cmd and for every match in my list I will call myCBConfig.callback.

Any good ideas how to make this work?

Ps. Im building a IRC library .Ds =)
 
Old 10-28-2003, 02:06 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Why wouldn't you give a name for your structs.
Code:
struct myCBConfig {
char* cmd; // Command to match
char* data; // Additional data to callback
void (*callback)(struct myCBConfig*, char* rawTcpData); // Callback to call
};
If you want to use typedef instead of struct name, you can write
Code:
typedef struct myCBConfig myCBConfig;
.

For more cleaner approach, I would write (YMMV)
Code:
struct myCBConfig;
typedef void CBFunction(struct myCBConfig*,char*);

struct myCBConfig {
  char* cmd; // Command to match
  char* data; // Additional data to callback
  CBFunction *callback;
};
A sample usage of this code would be
Code:
void aCBfunction(struct myCBConfig*cf,char *foo) {
  printf("Im useless..\n");
}

int main(void) {
  struct myCBConfig f;
  char c;
  f.callback=aCBfunction;
  f.callback(&f,&c);
  return 0;
}
The same thing in C++ would be
Code:
#include <cstdio>
class myCallback {
public:
  char* cmd; // Command to match
  char* data; // Additional data to callback
  virtual void operator()(char *rawTcpData)=0; //Callback to call
};

class aCallback : public myCallback {
void operator()(char *foo) { printf("Im useless..\n"); }
};

int main(void) {
  aCallback cb;
  myCallback &f=cb;
  char c;
  f(&c);
  return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM
struct pointer in C LuderForChrist Programming 2 01-07-2005 07:44 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration