Solved

Passing Macro as a function argument.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordmultiple challenge 12 131
Arduino EDI - Programming Language - Voice Recorder 4 81
Auto-indent certain lines in Notepad++ 10 73
draw a Christmas tree by using a nested loop? 26 61
This is about my first experience with programming Arduino.
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.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

813 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

14 Experts available now in Live!

Get 1:1 Help Now