?
Solved

Passing Macro as a function argument.

Posted on 2004-08-08
4
Medium Priority
?
356 Views
Last Modified: 2010-04-17
Hello Experts,

Can someone please tell me if there is a way to use macro (SQL1) as listed in my example below be passed as an argument to a function. If yes, then can you please tell me the function prototype and function call syntax.

Here is the code:

#include<stdio.h>

#define SQL1 "select col1,col2 from table1"           /* Sql statement */
int func(SQL1);          /* function protoype */

int main()
{
      func(SQL1);          /*Call to func - passing SQL1 as argument */

      return 0;
}  
int func(SQL1)
{
      printf(SQL1);        /* Print SQL1 contents */

      return 0;
}

Thanks!!
0
Comment
Question by:mohammadzahid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 400 total points
ID: 11745751
#include<stdio.h>

#define SQL1 "select col1,col2 from table1"           /* Sql statement */
int func(char*);          /* function protoype */

int main()
{
     func(SQL1);          /*Call to func - passing SQL1 as argument */

     return 0;
}  

int func(char* sql)
{
     printf(sql);        /* Print SQL1 contents */
     printf("\n");      // new line

     return 0;
}

0
 
LVL 3

Assisted Solution

by:teratoma
teratoma earned 400 total points
ID: 11745758
You should only use macros when there's no other way.  Here's how the compiler sees your code after the preprocessor si done with it:

int func("select col1,col2 from table1");

int main()
{
     func("select col1,col2 from table1");

     return 0;
}  
int func("select col1,col2 from table1")
{
     printf("select col1,col2 from table1");

     return 0;
}

A macro is not a function, variable, or anything that can be known by the compiler.  It's just a text search-and-replace tool.  You would be better off defining a constant string like this:

const char SQL1[] = "select col1,col2 from table1";

Then your code could look like this:

int func(const char*);          /* function protoype */

int main()
{
     func(SQL1);          /*Call to func - passing SQL1 as argument */

     return 0;
}  
int func(SQL1)
{
     printf(SQL1);        /* Print SQL1 contents */

     return 0;
}
0
 
LVL 3

Expert Comment

by:teratoma
ID: 11745766
of course it should look more like

int func(const char* sql)
{
     printf(sql);        /* Print SQL1 contents */

     return 0;
}
0
 
LVL 11

Author Comment

by:mohammadzahid
ID: 11749632
Your suggestions solved the problem and thanks for detailed explanation.
Since, both responses were very helpful therfore, I am going to split the point equally.

Thanks again!

Thread closed.


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question