You are here:

C++/how to pass paramters from c++ program to a macro

Advertisement


Question
I have a C++ program which runs a few macros, i need it to pass the username, pwd and another variable to the macro. How do i do that?

Answer
Hello meena,

First off, lets define a macro to use an an example here.
There is a couple different ways to do this. You can have the macro call a function, or you can do all the processes in the macro itself. First lets take a look at how to use a function in the macro. We can also use the preprocessor to determine which functions you want to use if it differs from debug and release configurations.  

#ifdef _DEBUG

#define STORE_DATA(name, password) FunctionDebug(name, password)

#else
#define STORE_DATA(name, password) FunctionRelease(name, password)

Here is the alternate way to do this and process the data in the macro itself.

// in main

// globals
struct Guy
{
char *name;
char *password;
};

Guy Bob;

#define STORE_DATA(name, password) strcpy(Bob.name, name); strcpy(Bob.password, password);

int main()
{
  const char *pName = "Bob";
  const char *pPassword = "Test";

  STORE_DATA(pName, pPassword);
  return 0;   
}

When you define a macro, it does not get a regular parameter list like a normal function. You define it with a word or letter or whatever you wish. Since a macro is nothing more than a copy and paste to the compiler, all works out fine. Also, a macro must be defined all on the same line. You can use the \ symbol which tells the compiler to skip to the next line for the readability sake of your code.

I hope this information was of help to you.

- Eddie  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.