Solved

Passing Macro as a function argument.

Posted on 2004-08-08
4
351 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
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 100 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now