Solved

Passing Macro as a function argument.

Posted on 2004-08-08
4
353 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Plain Text Editor for iPad 6 105
iSeries email authority 6 93
jboss 7.1 start up error 1 63
WordPress: Debugging from my Windows 10 Desktop 6 66
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

726 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