Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-01-2005, 09:14 AM   #1
LQ Newbie
Registered: Sep 2005
Posts: 9

Rep: Reputation: 0
how to pass pointer of struct to function?

I am trying to register a function to the Netdev_chain notifier call chain, in order to be notified every time a change in one of the ethernet adapters occurs. But I cant seem to make the register function work:

Output trying to compile:

[jc@localhost watcher]$ make -C /usr/src/kernels/`uname -r`-i686 SUBDIRS=$PWD modules
make: Entering directory `/usr/src/kernels/2.6.11-1.1369_FC4-i686'
CC [M] /home/jc/code/watcher/watcher.o
/home/jc/code/watcher/watcher.c: In function ‘watcher_init’:
/home/jc/code/watcher/watcher.c:73: error: invalid type argument of ‘unary *’
/home/jc/code/watcher/watcher.c: In function ‘watcher_exit’:
/home/jc/code/watcher/watcher.c:89: error: invalid type argument of ‘unary *’
make[1]: *** [/home/jc/code/watcher/watcher.o] Error 1
make: *** [_module_/home/jc/code/watcher] Error 2
make: Leaving directory `/usr/src/kernels/2.6.11-1.1369_FC4-i686'
[jc@localhost watcher]$

It basically isnt accepting any way I am trying to pass the struct notifier_block mynb into register_netdevice_notifier and unregister_netdevice_notifier...

int register_netdevice_notifier(struct notifier_block *nb);

my code (I hope I am not including too much code... )...

* watcher.c -
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h> /* For current */
#include <linux/tty.h> /* For the tty declarations */
#include <linux/version.h> /* For LINUX_VERSION_CODE */
#include <linux/notifier.h> /* For notification events */
#include <linux/netdevice.h> /* For notifier mechanism */


static void print_string(char *str)
struct tty_struct *my_tty;

/* The tty for the current task (for 2.6.6+ kernels) */
my_tty = current->signal->tty;

* If my_tty is NULL, the current task has no tty you can print to,
* so we do nothing
if (my_tty != NULL) {

* my_tty->driver is a struct located in tty_driver.h, which
* holds the tty's functions, one of which (write) is used to
* write strings to the tty.
* The function's 1st parameter is the tty to write to,
* because the same function would normally be used for all
* tty's of a certain type. The 2nd parameter is a pointer
* to a string. The 3rd parameter is the length of the string.
((my_tty->driver)->write) (my_tty, /* The tty itself */
str, /* String */
strlen(str) /* Length */
/* Send new line */
((my_tty->driver)->write) (my_tty, "\015\012", 2);

/* the Net Device Chain event handler function */
int notify_event_handler(struct notifier_block *self, unsigned long event , void *netdev)

// Do a switch here

return 0;

/* Declare and populate the structure */
struct notifier_block mynb={notify_event_handler, NULL, 1};

//&nb = &notify_event_handler();
//& = NULL;
//nb.priority = 1;

static int __init watcher_init(void)

print_string("The module has been inserted.");
print_string("Watcher module registering with Net Device Chain...");
/* Registering with Net Device Chain */
if ( register_netdevice_notifier( *mynb ) < 0 )
print_string("Net Device Chain registration failed!");
print_string("Net Device Chain registration successfull!");

return 0;

static void __exit watcher_exit(void)
print_string("Module unregistering from Net Device Chain...");
/* Unregistering with Net Device Chain */
if ( unregister_netdevice_notifier( *mynb ) < 0 ){
print_string("Net Device Chain unregistration failed!");
print_string("Net Device Chain unregistration successfull!");
print_string("Watcher module has been removed.");

Old 09-01-2005, 09:21 AM   #2
Registered: May 2002
Location: dracut MA
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 109Reputation: 109
The following needs to be changed:
if ( register_netdevice_notifier( *mynb ) < 0 )
if ( register_netdevice_notifier( &mynb ) < 0 )

if ( unregister_netdevice_notifier( *mynb ) < 0 )
if ( unregister_netdevice_notifier( &mynb ) < 0 )
If you have the K&R C, second edition, go to page 93 and read the whole thing.
Old 09-01-2005, 09:29 AM   #3
LQ Newbie
Registered: Sep 2005
Posts: 9

Original Poster
Rep: Reputation: 0
thanx for the quick reply...

I should be more carefull with pointers :/


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 07:26 AM
struct pointer in C LuderForChrist Programming 2 01-07-2005 07:44 AM
C: struct as a function parameter zokik Programming 3 12-06-2003 02:52 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:23 PM.

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