Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.std.c > #6213 > unrolled thread
| Started by | emanuele cannizzo <emacannizzo@gmail.com> |
|---|---|
| First post | 2021-02-07 09:30 -0800 |
| Last post | 2021-02-07 21:11 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.std.c
Problems with Parametric Macros emanuele cannizzo <emacannizzo@gmail.com> - 2021-02-07 09:30 -0800
Re: Problems with Parametric Macros James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-02-07 13:27 -0500
Re: Problems with Parametric Macros Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2021-02-07 21:11 +0100
| From | emanuele cannizzo <emacannizzo@gmail.com> |
|---|---|
| Date | 2021-02-07 09:30 -0800 |
| Subject | Problems with Parametric Macros |
| Message-ID | <db004614-55ee-4b5f-87c8-ed2513358d15n@googlegroups.com> |
In my code I have a lot of vectors with a similar name:
vector1, vector2, vector3, vector4, ...
Because I don't want to write the same code with the only difference of the names of vectors I tried creating this macro:
#define VECTOR(x) vector##x
I tried this code as an example:
int n = 1;
printf("%d", VECTOR(n)[0]);
I thought that was the same as printf("%d", vector1[0]); but the compiler substitutes the macro VECTOR(n)[0] with vectorn[0]
Can you give me a solution to this problem or suggest me other tecniques to use? Thanks
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2021-02-07 13:27 -0500 |
| Message-ID | <rvpbe0$8hl$1@dont-email.me> |
| In reply to | #6213 |
On 2/7/21 12:30 PM, emanuele cannizzo wrote:
> In my code I have a lot of vectors with a similar name:
> vector1, vector2, vector3, vector4, ...
> Because I don't want to write the same code with the only difference of the names of vectors I tried creating this macro:
>
> #define VECTOR(x) vector##x
>
> I tried this code as an example:
> int n = 1;
> printf("%d", VECTOR(n)[0]);
>
> I thought that was the same as printf("%d", vector1[0]); but the compiler substitutes the macro VECTOR(n)[0] with vectorn[0]
>
> Can you give me a solution to this problem or suggest me other tecniques to use? Thanks
The problem is that macro substitution occurs at compile time
(specifically, during translation phase 4), and it is purely textual.
As far as the preprocessor is concerned, "n" is just a character string,
which is why the result of the macro expansion is "vectorn". "n" doesn't
become the name of a variable with a value until translation phase 7,
and it doesn't become the variable itself (as opposed to the name of a
variable) until run time.
There's several alternatives you could use, one of which is
int *vector[] = {vector0, vector1, vector2, vector3};
and then
vector[n][0].
[toc] | [prev] | [next] | [standalone]
| From | Hans-Bernhard Bröker <HBBroeker@t-online.de> |
|---|---|
| Date | 2021-02-07 21:11 +0100 |
| Message-ID | <i8avshFs03eU1@mid.dfncis.de> |
| In reply to | #6213 |
Am 07.02.2021 um 18:30 schrieb emanuele cannizzo: > In my code I have a lot of vectors with a similar name: > vector1, vector2, vector3, vector4, ... Don't do that. As a rule of thumb, whenever you feel the urge to put running numbers of things into the name of things, you're almost certainly missing out on the obvious solution to make an array out of those things, instead, turning that number into an array index. You want vectortype vector[10]; // or however many of these you need Either that, you or you need to get up to speed with pointers to "vectortype" things.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.std.c
csiph-web