Solved

Passing Macro as a function argument.

Posted on 2004-08-08
4
350 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
Comment Utility
#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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

12 Experts available now in Live!

Get 1:1 Help Now