c-preprocessor

How to resolve int variable before passing to C/C++ Macros? [closed]

淺唱寂寞╮ 提交于 2019-12-02 13:39:03
I am trying to execute the following code: #define channel1 10 #define channel(id) channel##id int main(){ int id = 1; cout << channel(id)<<"\n"; return 0; } I get the following error: error: use of undeclared identifier 'channelid' Instead, I want an output to be 10 , as channel(id) should be preprocessed to channel1 and which replaces the value with 10. Is there any way to achieve it? The problem is caused because you're trying to mix information that it is considered at different stages in the processing of the code. Macros and all CPP (C-Pre-Processor) stuff happens (as its own name

Why does the NULL de-reference in this C snippet not cause undefined behaviour

落花浮王杯 提交于 2019-12-02 13:15:04
I came across a piece of code where NULL is typecast to an structure pointer type (foo *) 0 , and with that pointer de-referencing a member ((foo *)0)->m , and using address of that &(((foo *)0)->m)) and type casting it to integer to get the memory index of that member with in the structure. ((unsigned int)(&(((foo *)0)->m))) . To my knowledge NULL pointer dereference should always result a segmentation fault in C. But I don't understand how NULL pointer can be de-referenced like this and still not result in a segmentation fault. #include <stdio.h> #define MACRO(m) ((unsigned int)(&(((foo *)0)

Wrapping functions with macros (without renaming) C

梦想与她 提交于 2019-12-02 08:33:12
Im interested to add in some extra logic around existing function calls, by wrapping them without renaming them. (just for a test) . The existing solutions I found rely on wrapping a function in a macro of a different name, which can mean changing a lot of code. Any suggestions? Note, I'm aware of LD_PRELOAD , but am interested to use macro's to be able to inspect the arguments passed to the function (using _Generic for example). There's normally no need to rename macro-wrapped functions because inside the expansion of a macro, the macro's name is not expanded. Here's an example straight from

Compiling a project (VS 2008) with the /p argument (preprocess to a file) doesn't compile

最后都变了- 提交于 2019-12-02 07:33:03
I have a project in C++ that I would like to view the preprocessor output to see what some #defines and macros would look like. I tried the /p switch to turn on the preprocess to a file option to the compiler (it turns off full compilation and only runs the preprocessor) but my project now refuses to compile and shows a long list of errors starting with: "Cannot open include file: 'stdafx.h': No such file or directory". The project compiles fine without the /p argument, of course. Any suggestions? If you run cl.exe on its own then you would need to supply all the same parameters as the IDE

ANSI C: standard definition for the size of the __DATE__ and __TIME__ strings?

二次信任 提交于 2019-12-02 06:55:43
Is there a standard definition for the size of the __DATE__ and __TIME__ strings in ANSI C? The motivation behind this question is: I have two applications running on two different CPUs. During runtime, app #1 receives date and time (as part of version-info) from app #2. Of course, app #2 takes them from the preprocessor __DATE__ and __TIME__ definitions. So I would like to know whether or not I can statically allocate in app #1 an array, into which I can copy the info received from app #2. Thanks Vignesh Kumar A __DATE__ The date of translation of the source file (a character string literal

In a #define clause, how to make the preprocessor replace a parameter inside a variable name?

懵懂的女人 提交于 2019-12-02 06:48:34
问题 I have the following code: #define MY_MACRO(PARAM) int PARAM_int; double PARAM_double; [subsequent instructions] Unfortunately, it does not work, meaning that PARAM is not replaced inside the variables names. Is this solvable some way? 回答1: PARAM_int is considered to be a single token, that is distinct from PARAM . You can concatenate tokens in a macro definition with ## : #define MY_MACRO(PARAM) int PARAM ## _int; double PARAM ## _double; Now, PARAM will expand to whatever you invoke the

Syntax error while using a #define in initializing an array, and as arguments to a function in C? [closed]

╄→尐↘猪︶ㄣ 提交于 2019-12-02 06:40:13
Using a #define while initializing an array #include <stdio.h> #define TEST 1; int main(int argc, const char *argv[]) { int array[] = { TEST }; printf("%d\n", array[0]); return 0; } compiler complains: test.c: In function ‘main’: test.c:7: error: expected ‘}’ before ‘;’ token make: *** [test] Error 1 Using a #define as functional input arguments #include <stdio.h> #define TEST 1; void print_arg(int arg) { printf("%d", arg); } int main(int argc, const char *argv[]) { print_arg(TEST); return 0; } compiler complains: test.c: In function ‘main’: test.c:12: error: expected ‘)’ before ‘;’ token make

Putting loop inside C macro

和自甴很熟 提交于 2019-12-02 06:31:08
问题 I'm looking for a way to convert the following function structure to a macro. I know, it's a silly and pointless example, but it illustrates the point since I cannot give out my actual source code. int foo(int x, int y) { do { --x; ++y; }while(x > y); return x * y; //note that x and y have changed values here. } So that I can call the function in main or some other function like so: int next_x = foo(x,y); I cannot seem to get the syntax 100% correct here. This is my poor attempt: #define FOO

What is WINAPI_FAMILY_ONECORE_APP?

落爺英雄遲暮 提交于 2019-12-02 06:17:41
问题 I was looking through Microsoft's port of OpenSSL on GitHub. One commit caught my eye, and it was Adding Win10 Universal Platform support. In the commit, a partition called WINAPI_FAMILY_ONECORE_APP showed up. However, I'm not finding much about it when searching. There are two hits in English and 22 hits in Chinese (see below). Following What’s new in Visual Studio Tools for Windows 10 Preview provides some quasi-bullet points with no explanations: new API partition WINAPI_FAMILY_ONECORE_APP

#ifndef in c file?

戏子无情 提交于 2019-12-02 06:12:55
问题 Is it possible to put #ifndef at the top of a c file? Basically I need to check whether a certain preprocessor constant was declared when running the program and my program will change accordingly. I need to check if -D DESCENDING_ORDER=1 is added as an argument (doesn't matter what value given). I have this code at the top of my main c file: #ifndef DESCENDING_ORDER int ascending = 1; #else int ascending = 0; #endif Works when compiling by itself, but I get errors when I try compiling with a