LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-21-2011, 12:26 PM   #1
minivy
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Rep: Reputation: Disabled
Understand Define Macro in moduleparam.h in C


To Linux friends,

I have hard time to understand the Define macros in the moduleparam.h header file. I hope someone can help me out. Below is the code

#define module_param(name, type, perm) \
module_param_named(name, name, type, perm)

#define module_param_named(name, value, type, perm) \
param_check_##type(name, &(value)); \
module_param_cb(name, &param_ops_##type, &value, perm); \
__MODULE_PARM_TYPE(name, #type)

Question1: What does the module "module_param_named(name, name, type, perm)" in the first define do?

Question2: Why the same module name (module_param_named) is redefined in the second define?

Question3: What does the character ";" do in the second define statement?

Thanks,

minivy
 
Old 11-21-2011, 01:31 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194
Quote:
Originally Posted by minivy View Post
I have hard time to understand the Define macros
A macro is a text substitution, which is done before most of the compilation process.

Quote:
Question1: What does the module "module_param_named(name, name, type, perm)" in the first define do?
That is the thing that is substituted. Since name, type and perm are parameters of the macro, those are substituted from the actual text of the macro invocation at the seem time this whole definition module_param_named(name, name, type, perm) is substituted for the whole invocation of the macro

Quote:
Question2: Why the same module name (module_param_named) is redefined in the second define?
It isn't the same macro name (module_param vs. module_param_named).

The second definition defines the thing used by the first definition. That sequence of definitions is correct for macros though the same sequence would wrong for functions (without pre declaring) as long as the first actual use outside of a define is after all the parts are defined.

As for why the author had one define use another, I'd need to see the whole project to make a decent estimate of author intent (but that isn't an invitation to show me the whole project). But do you understand why a function might call another function rather than do its job directly? Same reasons can apply for a macro.

Quote:
Question3: What does the character ";" do in the second define statement?
From the point of view of the macro, it is just part of the text that is substituted. After the macro substitution occurs, that ; is a statement terminator understood by the next stage of compilation.

Last edited by johnsfine; 11-21-2011 at 01:42 PM.
 
1 members found this post helpful.
Old 11-21-2011, 03:15 PM   #3
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
The nested macro calls expand the parameters again. Doing that means that if the arguments to the outer macro are themselves macros, the stringize and token-pasting operators will work on those macros' values, not their names.
Code:
#include <stdio.h>

#define A Hello
#define B World
#define S_(a,b) a ## b
#define S(a,b) S_(a,b)

int main() 
{
        const char *HelloWorld = "Hello, World!";
        const char *AB = "ABooBoo";
        puts(S_(A,B));
        puts(S(A,B));
        return 0;
}
 
2 members found this post helpful.
Old 11-21-2011, 06:35 PM   #4
minivy
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thanks for the reply. They are very helpful.
 
Old 11-21-2011, 06:57 PM   #5
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
You're welcome. I was sitting thinking "I _know_ there's something missing with that explanation", but it took me a while to realize what it is: it isn't exactly that the second call causes the arguments to be expanded again but that stringize and paste cause them to not be expanded at all (though the result of a paste will be).

If you're not into heavy-duty preprocessor abuse please ignore this post.
 
  


Reply


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
What does ## sign do in moduleparam.h? hiendang Linux - General 1 11-20-2011 01:50 AM
Macro #if versus #define question in C dman777 Programming 4 08-08-2011 03:02 PM
Redefnition of struct kernel_param error in moduleparam.h keshu_lk Linux - Kernel 2 11-09-2006 12:51 AM
[C] #define/ #ifdefine macro newbie questions noir911 Programming 1 04-07-2006 09:22 AM
Sendmail System identity & define macro value sysxi Linux - Software 0 04-22-2004 09:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:25 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration