On Wed, Aug 01, 2018 at 05:29:53PM -0400, Ruben Safir wrote:
And the K&R book on C does successive trimming of an strcpy(), and finishes with
void strcpy(char *a, *b) { while (*a++ = *b++); }
The thing that's actually used a lot in the Linux kernel that gives a lot of C newcomers heartburn is the widespread use of structures of function pointers.. For example, this from include/linux/fs.h:
struct lock_manager_operations { int (*lm_compare_owner)(struct file_lock *, struct file_lock *); unsigned long (*lm_owner_key)(struct file_lock *); fl_owner_t (*lm_get_owner)(fl_owner_t); void (*lm_put_owner)(fl_owner_t); void (*lm_notify)(struct file_lock *); /* unblock callback */ int (*lm_grant)(struct file_lock *, int); bool (*lm_break)(struct file_lock *); int (*lm_change)(struct file_lock *, int, struct list_head *); void (*lm_setup)(struct file_lock *, void **); };
That's the structure definition. Novice challenge: Find one or more places where this structure is initialized, and understand how and why that works.
That kind of stuff is not taught in C programming. An answer can be very educating.
https://en.cppreference.com/w/c/language/pointer#Pointers_to_functions If you wish to see its use: refer iio drivers and grep for read_raw and write_raw. Hope this helps. -- Himanshu Jha Undergraduate Student Department of Electronics & Communication Guru Tegh Bahadur Institute of Technology