C :: Writing A Macro That Returns A Boolean Value

Apr 24, 2014

I need writing a macro that would return true/false (1/0) )value. I want to check if a certain element exists in the array. The macro will accept array, its size, and the value to be compared, and must return yes or no. Here is the code that I have written:

Code:
#define EXISTS(T, a, n, val) do {
char ret=0;
T *a_ = (a);
size_t n_ = (n);
for (; n_ > 0; --n_, ++a_){
ret = (*a_ == val);
}
}
while(0)

How can I get the result from this macro.

View 6 Replies


ADVERTISEMENT

C++ :: Recursive Boolean Function - Compare Two Stacks And Returns True If Identical

Oct 21, 2014

The question is to write a recursive boolean function that compares two stacks and returns true if they are identical. This is where I get stuck:

If the top items of both stacks are the same, the recursive call always returns true, because 'true' is saved on top of the return stack.

Here is my code:
template<class Type>
bool identicals(stackType<Type> s1, stackType<Type> s2) {
if(!s1.isEmptyStack() && !s2.isEmptyStack()) {
if(s1.top() != s2.top())

[Code] ....

View 2 Replies View Related

C++ :: Define A Macro Within A Macro?

Feb 26, 2012

Is it possible to define a macro with in a macro? Any trick will do. I am trying to do quick conversion of cuda program to open mp by defining some macros at the top:

Code:
#define __syncthreads() #pragma omp barrier

View 2 Replies View Related

C :: Defining And Using A Macro

Jan 4, 2015

I am trying to create a small set of filepath functions that I intend to compile across linux and windows (I prefer not to use a big library). I want to have a global constant PATH_SEPARATOR that depends on the OS environment. This is what I set at the top of header file.

Code:

#include <stdio.h>
const char PATH_SEPARATOR =
#ifdef _WIN32
'';
#else
'/';
#endif I was hoping to test this while compiling this in a linux environment using gcc, thusly:

Code:

int main (int argc, char const* argv[])
}

[code]....

where apparently, I seem not to be able to "set" a part of the code to have "_WIN32" defined. I don't know if I explained this clearly.

View 5 Replies View Related

C++ :: Macro To Template?

Mar 22, 2013

I have this macro, that expands to some methods of my class:

Code:
#define POPULAR_COMBO_FILTRO_COM_BASE(classeTabela)
void VisualizadorLogsFrame::PopularComboFiltroCom ## classeTabela (bool limparTexto) {
long from, to;
m_cbxDetalhe->GetSelection(&from, &to);

[Code]...

but I get a compile error on this line:

Code:
for (list< CLASSE_TABELA *>::iterator linha = lista->begin(); linha != lista->end(); linha++) {

what am I doing wrong ? the error is:

VisualizadorLogsMain.h:174: error: expected ';' before 'linha'
VisualizadorLogsMain.h:174: error: 'linha' was not declared in this scope

View 4 Replies View Related

C :: Variadic Macro Function

Sep 12, 2013

I'm doing right now is creating a function that callocs (I prefer this to malloc) and returns a string, and it will work similar to printf, I'm calling the function alloCpy(),I have several values that I need in a malloced string, so I call Code: myAllocedString = alloCpy("Value 1 is %s, value 2 is %s, and value 3 is %d", str1, str2, num); To do this I'm using the Variadic Macro, the reason I'm not just using a Variadic Function such as this: Code: char* alloCpy(char *format, ...) {} is because I need to append NULL to the end for the sake of looping through arguments, and I'm understanding it thusfar, but I have a few issues, first of all, I tried defining the Macro in a header file, but when I try to call it I get the error "Undefined reference to alloCpy". Also, to loop through arguments to get string lengths I'm using va_arg(args, char*) which requires all the arguments to be of type char*. Here is my code:
myheader.h:

Code:

#define alloCpy(format, ...) _alloCpy(format, ##__VA_ARGS__, NULL);
char* _alloCpy(char *format, ...); mycfile.c: Code: char* _alloCpy(char *format, ...) {
va_list args;
va_start(args, format);
int args_len = 0;
}

[code]....

So, how can I do this to, first of all, make my macro function accessible from other files importing myheader.h, and second, how can I make it accept any type of argument like printf, so that my example above would work?

View 3 Replies View Related

C :: Testing If A Macro Is Defined To A Value?

Jan 16, 2015

Say I have something lke

Code:
#define FOO BAR
#if FOO == BAR
doX();
#else
doY();
#endif

This causes doX(); to be executed. But the intent is to have doY(); be run. I'm guessing this is because BAR is undefined and therefore blank, so blank equals blank. Is there some way to compare the symbol FOO was set to instead of its value, BAR?

View 4 Replies View Related

C++ :: Replacing Macro With Constant

Apr 4, 2013

I heard that const shall be preferred over #define . So I start to change my program accordingly.

But then below error message occurs during compilation:

#include "common.h"
#include "definition.h"
#include "particle.h"
int main() {
Particle *p = new Particle();

[Code] .....

I guess the error occurs because, when the line 9 of particle.h (File 4) is compiled, value of const int dimension is not seen by the compiler.

View 6 Replies View Related

C/C++ :: Migration Of Serialization Macro

Jun 12, 2014

I have to migrate C++ source which is compiled with MinGW-gcc to be compiled with VS2013, and have one issue with one macro used for serialization.

Here is macro:

#define IMPLEMENT_SERIALIZE(statements)
unsigned int GetSerializeSize(int nType, int nVersion) const
{
CSerActionGetSerializeSize ser_action;
const bool fGetSize = true;

[Code] ....

Here is place in code were it is call (in class declaration):

IMPLEMENT_SERIALIZE(({
if (!fRead) {
uint64 nVal = CompressAmount(txout.nValue);
READWRITE(VARINT(nVal));

[Code] .....

When compile received errors:

main.h(555): error C2059: syntax error : '{'
main.h(555): error C2143: syntax error : missing ';' before '{'
line 555 is end of macro call ( "})").

How should look using of this macro in order to avoid that errors?

View 7 Replies View Related

C++ :: Pitfalls Of Macro Substitution?

Dec 29, 2013

"If you examine the expansion of max, you will notice some pitfalls. The expressions are evaluated twice; this is bad if they involve side effects like increment operators or input and output. For instance, the below example will increment the larger twice."

#define max(A, B) ((A) > (B) ? (A) : (B))

max(i++, j++)/* WRONG */

I don't see what the problem is with the code above. i is incremented and j is incremented and then it performs a ternary operation to see which is greater. Am I missing something?

View 1 Replies View Related

C++ :: Two Projects - Defining Value Of Macro

Jun 27, 2012

Say I have two projects A and B. A depends on B. If project A defines a macro to be 100 and project B defines the same macro to be 200. In project A, if I use this macro, what value would this macro be? Let's just forget macro is evil for the time being. Let's also forget that it is not good to define the same macro twice for the time being.

View 8 Replies View Related

C :: Boolean Value Use For If And Else Statements

Apr 20, 2013

Am i using boolean values correctly? our professor wanted boolean value use for the if and else statements.

Code:
#include <stdio.h>
#include <math.h>
#define GRAVITY 9.8
#define LIMIT 500
#define SEC 5

[Code] ....

View 2 Replies View Related

C :: How Macro Works To Concatenate Three Integers

Nov 15, 2014

when i was finding a way to concatenate three integers in C, I came across a snippet like this

Code:

#include<stdio.h>
#define cat(a,b,c) a##b##c
int main()
{
int d;
d=cat(1,2,3);
return 0;
}

How the macro works here? I am unable to understand the function '#' plays in the macro.

View 1 Replies View Related

C++ :: Macro For Changing Array Size

Nov 22, 2013

I'm trying building a new macro for change the array size:

#define redim(varname,x) ((sizeof(varname)*) realloc (varname, x * sizeof(varname)));
int b;
redim(b,3);

error message:
"error: expected primary-expression before ')' token"

what isn't right with these macro?

View 10 Replies View Related

C++ :: Linking To Static Library That Contains A Macro

Jul 16, 2014

I have built a static library that contains a macro. From a separate exe, I am calling this macro, but it doesn't work.

The compilation of the static library and that of the exe went okay.

Static lib contains just the macro definition.

Exe contains call to this macro.

Is there anything else that I need to do for this to work?

View 4 Replies View Related

C# :: Command To Attach A Macro On A Process?

Jan 20, 2014

i would like to know if its possible to use a c# command to attach a macro ( JitBix MacroRecorder ) so it would send keystrokes while the process/program was minimized. i've tried with [lapeiro, on 20 January 2014 - 06:43 AM, said: i would like to know if its possible to use a c# command to attach a macro ( JitBix MacroRecorder ) so it would send keystrokes while the process/program was minimized.

View 4 Replies View Related

C++ :: Adding Char Array With Macro?

Dec 30, 2013

The book uses this example:

#define ALLOCSIZE 10000 /* size of available space */
static char allocbuf[ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf; /* next free position */
char *alloc(int n)
/* return pointer to n characters */

[Code] ....

The logic here I don't understand:

if (allocbuf + ALLOCSIZE - allocp >= n)

allocbuf is a char array allocated with 10000 positions. We add that to ALLOCSIZE macro which is 10000. Does that return a value of 20000? If so, it doesn't make sense to do this because all we had to do was this:

if (allocbuf - allocp >= n)

That takes length of allocbuf which is 10000 and subtracts allocp from it. So if allocp is 1000 we are left with 9000 available slots. And we can use that to check if we have enough to allocate n elements.

View 5 Replies View Related

Visual C++ :: Using Macro To Generate DEF File

Nov 30, 2012

I'm trying to build libglib using MSVC. libglib doesn't use dllexport - so to create usable DLLs it needs to generate a .DEF file. It does this by using a .symbols file. Here's a cut-down example:-

Code:
/* This file lists all exported symbols. It is used to generate the glib.def file used to control exports on Windows.*/

/* glib.symbols */
g_mkdir_with_parents

#ifdef G_OS_WIN32
g_file_get_contents_utf8
#endif

The following commands would produce the following files called glib.def

Code:
cl /EP glib.symbols > glib.def

produces this in glib.def:-
g_mkdir_with_parents

Code:
whereas this:-
cl /EP -DG_OS_WIN32 glib.symbols > glib.def

produces this in glib.def:-
g_mkdir_with_parents
g_file_get_contents_utf8

So this is a handy way of producing conditional .DEF files. Fortunately, this can all be built into a Visual Studio macro in my vsprops file - e.g.

Code:
<UserMacro
Name="GlibGenerateGlibDef"
Value="echo >"$(SolutionDir)glib.def" && cl /EP -DG_OS_WIN32 glib.symbols >>"$(SolutionDir)glib.def""
/>

Problem:- Although I can run the macro as a pre-build step (and it does produce conditional entries in the generated .DEF file) it DOESN'T produce the word EXPORTS at the top of the file. So the linker then fails to recognise it as a valid .DEF file.

How can I add that word EXPORTS to the top of the generated file? I tried just adding EXPORTS to the top of the symbols file but that didn't work. I also tried this variation on the macro:-

Code:
Name="GlibGenerateGlibDef"
Value="echo EXPORTS >"$(SolutionDir)glib.def" && cl /EP -DG_OS_WIN32 glib.symbols

But that didn't work either I feel sure there must be a way to do this.

View 1 Replies View Related

C++ :: Macro For Debugging New Through Operator New Syntax?

Aug 14, 2013

It's common to see the following macros:

Code:
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW

and overloading operator new for debugging purposes. It usually works but there is a problem when use operator new syntax in code instead of directly use new. I can change my code but i can't change third party's code. Something like this:

Code:
int* j = static_cast<int*>(::operator new(sizeof(int)));

It's not common, but it appears occassionally.With that syntax, new macro generates compilation errors.

Is there any way to fix this problem?

View 2 Replies View Related

Visual C++ :: Macro With Multiple Parameters

Oct 22, 2014

In the following example, SET_DARKENING_PARAMETERS_0 is a fairly simple macro which accepts 9 parameters. The first one is called driver and the other 8 are all ints. Basically, the 8 x ints get assigned to the first 8 elements of an array which is owned by driver. I can call the macro successfully if I do this:-

Code: SET_DARKENING_PARAMETERS_0 ( some_driver, 500, 400, 1000, 275, 1667, 275, 2333, 0 );

But in the actual code I'm trying to compile, the 8 x ints are themselves encoded into a secondary macro like this:-

Code: #define CONFIG_OPTION_DARKENING_PARAMETERS 500, 400, 1000, 275, 1667, 275, 2333, 0

So the actual code (to call SET_DARKENING_PARAMETERS_0) looks like this:-

Code: SET_DARKENING_PARAMETERS_0 ( some_driver, CONFIG_OPTION_DARKENING_PARAMETERS );

But the two combined macros won't compile with VC8. Basically, I get warning #C4003 (not enough parameters for macro 'SET_DARKENING_PARAMETERS_0') followed by a load of syntax errors. However, the code (allegedly) compiles with gcc.

I'm wondering if macro CONFIG_OPTION_DARKENING_PARAMETERS might be getting truncated to just 500, (with the following parameters getting ignored)?

View 5 Replies View Related

C++ :: Negating Boolean Values

Mar 22, 2013

I made some research on how to negate a boolean value but none seemed to work. Ok, so I have the following code:

Code:

#include <iostream>
using namespace std;
int main() {
bool Turn = true;
for (int i = 1; i < 11; i++)

[Code]....

As of now the code prints "XXXXXXXXXX". And I want it to print "XOXOXOXOXO", so I tried 2 different things, which did not work:

Code:

#include <iostream>
using namespace std;
int main()
{
bool Turn = true;
for (int i = 1; i < 11; i++)
{

[Code]...

Code:

#include <iostream>
using namespace std;
int main()
{
bool Turn = true;
for (int i = 1; i < 11; i++)

[Code]...

Ok so I made those changes so that the boolean 'Turn' would change everytime the loop executed. Guess it doesn't work.

View 2 Replies View Related

C++ :: Casting A Logical Boolean As Int?

Jun 8, 2013

Let's say I have a product that needs service after 500 hours at 100 RPM. I can dsiplay the remaining hours only as an integer and we only have integer math. Management wants it to display 500 for the first hour, not 499 after the first minute is ticked off. But after a few minutes fullHours will be 499 and partialHours will have some value. I have 3 ways to determine displayHours to suit management and I want to know if the first is ligit.

short fullHours = 500;
short partialHours = 0;
short displayedHours = 0;

// Method 1 - Is is Kosher to cast a boolean as an int? Is TRUE always a 1 or is that a bad assumption?

displayedHours = fullHours + (short) (partialHours != 0);

//Method 2 - Works but some have disdain for the ternary conditional

displayHours = fullHours + (partialHours ? 1 : 0);

//Method 3 - seems a bit pedantic

displayHours = fullHours;
if (partialHours != 0) {
displayHours++;
}

View 19 Replies View Related

C++ :: Boolean Function In Turbo?

Jan 9, 2015

Is the boolean function already defined under the default headers? Else, how would I create one? Would this work? :

#define true 1
#define false 0
typedef int bool;
Bool x=true;

View 3 Replies View Related

C++ :: 2D Array In Boolean Function

Apr 24, 2014

i have a Boolean function containing 2D dynamic array, it'll retain either 0 or 1, how can i delete the dynamic array?

bool something (int** a,int b, int c) {
int **arr = new int*[b];
for(int i=0;i<b;i++)
arr[i]= new int[c];
if (...) return 0;
else ...
if (...) return 0;
}

View 3 Replies View Related

C# :: Split String With Boolean

Nov 19, 2014

I have my Arduino send the following string every second:

69.4,69.4,69.4,69.4,69.4,69.4,8.42,100,100,50,50,20,16,10,14

and i currently have my C# code split it and send the values to corresponding text boxes:

// SERIAL READS:
private void myport_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) {
string RxData = myport.ReadLine();
this.BeginInvoke(new LineReceivedEvent(LineReceived), RxData);

[Code] ....

Now if i were to add true/false values to my arduino string, im not sure to read it. I would like the bool values to enable/disable led jpegs to simulate output status.

rly1_led.enable = newData[15];// if true

Ive tried everything and just cant figure it out. This is my first C# project.

View 14 Replies View Related

C++ :: What Is The Type Specifier For Boolean

Dec 11, 2012

In the following example, how 'd I use sprintf when I have a boolean type of variable?

Code:
char s[64];
bool bVar = false;
sprintf(s, "This is a boolean variable bVar = %?", bVar);

What is supposed to replace ? in the sprintf statement ?

View 10 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved