By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Named variable in a list?

P: n/a
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:

int a, b, c;
int list[] = {&a, &b, &c};

But I only want to declare 'a', 'b' and 'c' once (they're declared 2
time here, one for the instance and one for the pointer in the list).
Now I figure it's possible to do that using a macro, but I just can't
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();

You'd need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven't seen).

Thnx

Aug 11 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Jo**************@gmail.com wrote:
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:

int a, b, c;
int list[] = {&a, &b, &c};

But I only want to declare 'a', 'b' and 'c' once (they're declared 2
time here, one for the instance and one for the pointer in the list).
Um. I don't think that's quite right. This is going to give you
compiler
diagnostics about converting from int * to int. You should change this
to one of the following.

int * list[] = {&a, &b, &c};
int list[] = {a,b,c};

Though, with the second, you want to initialize a, b, and c before
you use the values to fill in the array.

Also, it does not declare a, b, and c twice. Assuming it actually
worked, the declaration of a is on the line

int a,b,c;

and nowhere else.

Also, "list" is a poor name for an array.
Now I figure it's possible to do that using a macro, but I just can't
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();

You'd need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven't seen).
It's really not clear what it is that you want to do that would not be
done by cleaning up your original example. Can you explain differently
why the original would not be acceptable?
Socks

Aug 11 '06 #2

P: n/a
Jo**************@gmail.com wrote:
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:

int a, b, c;
int list[] = {&a, &b, &c};
You can't put pointers to ints in an array of ints.
Either:
int a, b, c ;
int * list[] {&a, &b, &c} ;

or:
int list[3] ;
>
But I only want to declare 'a', 'b' and 'c' once (they're declared 2
time here, one for the instance and one for the pointer in the list).
Now I figure it's possible to do that using a macro, but I just can't
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();

You'd need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven't seen).

Thnx
"Macros are evil."

It looks like you are trying to do something like:

{
int a ;
list[0] = &a ;
}

But then when that scope exits your pointer is no longer valid anyway,
so trying to use it is undefined behavior.

Somewhat unrelated, since if I'm right about what you are trying to do,
you shouldn't use this anyway, but if you need to create temporaries in
a macro, a common trick is something like:
#define MYMACRO \
do \
{ \
} while(0)

The do/while(0) introduces a scope you can put stuff in, and also is
syntactically correct if you put a semicolon after a use of your macro.

--
Alan Johnson
Aug 11 '06 #3

P: n/a
Ok, my mistake I meant:

int * list[] = {&a, &b, &c};

in the original post (google groups don't give compiler warnings duh).

I know 'a' does get declared only once; all I'm saying is if you want
to add something you gotta type it twice (declaration and adding to the
list). And uhm, list being a poor name for an array, I couldn't care
less; this is just a dumb example.

Hope this clarifies the thing.

Puppet_Sock wrote:
Jo**************@gmail.com wrote:
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:

int a, b, c;
int list[] = {&a, &b, &c};

But I only want to declare 'a', 'b' and 'c' once (they're declared 2
time here, one for the instance and one for the pointer in the list).

Um. I don't think that's quite right. This is going to give you
compiler
diagnostics about converting from int * to int. You should change this
to one of the following.

int * list[] = {&a, &b, &c};
int list[] = {a,b,c};

Though, with the second, you want to initialize a, b, and c before
you use the values to fill in the array.

Also, it does not declare a, b, and c twice. Assuming it actually
worked, the declaration of a is on the line

int a,b,c;

and nowhere else.

Also, "list" is a poor name for an array.
Now I figure it's possible to do that using a macro, but I just can't
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();

You'd need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven't seen).

It's really not clear what it is that you want to do that would not be
done by cleaning up your original example. Can you explain differently
why the original would not be acceptable?
Socks
Aug 11 '06 #4

P: n/a
posted:
I want to declare variables while
adding them to a list simultaneously.

Are you trying to achieve something like the following?

(Written sloppily with haste, may contain a bug or two:)

#define nullptr 0 /* Until the next Standard */

#include <iostream>
using std::cout;

template<class T>
struct LinkedPointer {
T *p;
LinkedPointer *pnext;
};

class Chain {
private:
int first_int;
LinkedPointer<intfirst_link;

public:

Chain() : first_int(-999)
{
first_link.p = &first_int;
first_link.pnext = nullptr;
}

Chain &Register(int &var)
{
/* This algorithm is inefficient, but it does the trick */

LinkedPointer<int**p;

for(p = &first_link.pnext; *p; p = &(*p)->pnext) /* No body */;
/* This loop brings it until the next nullptr */

*p = new LinkedPointer<int>;

(*p)->p = &var;
(*p)->pnext = nullptr;

return *this;
}

void PrintAllRegistered() const
{
cout << first_int << '\n';

for(LinkedPointer<int*const*p = &first_link.pnext; *p; p = &
(*p)->pnext)
cout << *(*p)->p << '\n';
}
};

Chain global_chain;

void SomeFunc()
{
int static i = 67;

global_chain.Register(i);
}

int main()
{
int a = 5, b = 22, c = 656;

global_chain.Register(a).Register(b).Register(c);

SomeFunc();

int &d = *new int(13);

global_chain.Register(d);

global_chain.PrintAllRegistered();

delete &d;
}

--

Frederick Gotham
Aug 11 '06 #5

P: n/a
Jo**************@gmail.com wrote:
Puppet_Sock wrote:
Jo**************@gmail.com wrote:
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:
>
int a, b, c;
int list[] = {&a, &b, &c};
>
But I only want to declare 'a', 'b' and 'c' once (they're declared 2
time here, one for the instance and one for the pointer in the list).
Um. I don't think that's quite right. This is going to give you
compiler
diagnostics about converting from int * to int. You should change this
to one of the following.

int * list[] = {&a, &b, &c};
int list[] = {a,b,c};

Though, with the second, you want to initialize a, b, and c before
you use the values to fill in the array.

Also, it does not declare a, b, and c twice. Assuming it actually
worked, the declaration of a is on the line

int a,b,c;

and nowhere else.

Also, "list" is a poor name for an array.
Now I figure it's possible to do that using a macro, but I just can't
figure out how, ie:
>
declare(list, int, a);
declare(list, int, b);
declare(list, int, c);
>
declare_instances();
declare_list_pointers();
>
You'd need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven't seen).
It's really not clear what it is that you want to do that would not be
done by cleaning up your original example. Can you explain differently
why the original would not be acceptable?
Socks

Ok, my mistake I meant:

int * list[] = {&a, &b, &c};

in the original post (google groups don't give compiler warnings duh).

I know 'a' does get declared only once; all I'm saying is if you want
to add something you gotta type it twice (declaration and adding to the
list). And uhm, list being a poor name for an array, I couldn't care
less; this is just a dumb example.

Hope this clarifies the thing.
Please don't top post.
http://www.parashift.com/c++-faq-lit...t.html#faq-5.4

Perhaps it would help if you explained what problem you are trying to
solve. Creating a bunch of ints and then putting pointers to them in
an array doesn't seem particularly useful. If you want an array of
ints just declare one. If, perhaps, you want a list, look into
std::list or std::vector.

--
Alan Johnson

Aug 11 '06 #6

P: n/a
Frederick Gotham posted:
void PrintAllRegistered() const
{
cout << first_int << '\n';

for(LinkedPointer<int*const*p = &first_link.pnext; ...

LinkedPointer<intconst *const *p

--

Frederick Gotham
Aug 11 '06 #7

P: n/a
Frederick Gotham wrote:
Frederick Gotham posted:
void PrintAllRegistered() const
{
cout << first_int << '\n';

for(LinkedPointer<int*const*p = &first_link.pnext; ...


LinkedPointer<intconst *const *p

--

Frederick Gotham

Uhm, look, what I'm asking is simple. I want to do this:

int a, b;
list.add(&a); list.add(&b);

with something like this:

declare_and_add(a);
declare_and_add(b);

Now, fill in the blank for 'declare_and_add' (either with a #define,
template, whatever). Don't ask about the particular context -- this is
a totally out of context, _simple_ problem (not that the solution is).

Aug 11 '06 #8

P: n/a
Jonathan posted:
Uhm, look, what I'm asking is simple.

Magnificient tone of voice. I think I'll be sadistic and not help you.

I want to do this:

int a, b;
list.add(&a); list.add(&b);

with something like this:

declare_and_add(a);
declare_and_add(b);

Now, fill in the blank for 'declare_and_add'

Do I've to have this done for you by 5pm on Thursday?

(either with a #define, template, whatever).

#define JonathanFillion Arrogant Asshole

or perhaps:

struct JonathanFillion {};

template<class ArrogantAsshole>
void TempFunc()
{

}

void (&Func)() = TempFunc<JonathanFillion>;

Don't ask about the particular context -- this is
a totally out of context, _simple_ problem (not that the solution is).

The solution is *extremely* simple. Pity I'm too sadistic to show you how
macros work.

--

Frederick Gotham
Aug 11 '06 #9

P: n/a
Frederick Gotham wrote:
Jonathan posted:
Uhm, look, what I'm asking is simple.


Magnificient tone of voice. I think I'll be sadistic and not help you.

I want to do this:

int a, b;
list.add(&a); list.add(&b);

with something like this:

declare_and_add(a);
declare_and_add(b);

Now, fill in the blank for 'declare_and_add'


Do I've to have this done for you by 5pm on Thursday?

(either with a #define, template, whatever).


#define JonathanFillion Arrogant Asshole

or perhaps:

struct JonathanFillion {};

template<class ArrogantAsshole>
void TempFunc()
{

}

void (&Func)() = TempFunc<JonathanFillion>;

Don't ask about the particular context -- this is
a totally out of context, _simple_ problem (not that the solution is).


The solution is *extremely* simple. Pity I'm too sadistic to show you how
macros work.

--

Frederick Gotham
Yew, I hope that's not how you talk to your co-workers when they ask
you a question and you don't succeed in giving them a relevant answer.

Chill out, all I was saying is you don't need to paste me your kernel's
source code to answer me; what I'm looking for should be only a couple
of lines.

Don't get heated up for that, for fsck's sake.

Aug 11 '06 #10

P: n/a
Jo**************@gmail.com wrote:
I want to do this:

int a, b;
list.add(&a); list.add(&b);

with something like this:

declare_and_add(a);
declare_and_add(b);

Now, fill in the blank for 'declare_and_add' (either with a #define,
template, whatever). Don't ask about the particular context -- this is
a totally out of context, _simple_ problem (not that the solution is).
You could do it various ways, but all the ways that I can think of are
uglier than your original code. Tell us how and why you want to use
this (and cf.
http://parashift.com/c++-faq-lite/ho....html#faq-5.8), and
perhaps we can suggest a different way altogether to achieve the same
end.

Cheers! --M

Aug 11 '06 #11

P: n/a
Jonathan Fillion posted:
Yew, I hope that's not how you talk to your co-workers when they ask
you a question and you don't succeed in giving them a relevant answer.

I don't have co-workers. If I did, and if they were arrogant assholes, I'd
intercept them on their way to the carpark and relate to them how I don't
like them, and how it's in their best interest to stay out of my way.

Rethink your usage of language when someone voluntarily offers you some
help -- "succeed" doesn't convey appreciation.

Chill out, all I was saying is you don't need to paste me your kernel's
source code to answer me; what I'm looking for should be only a couple
of lines.

You've the wrong attitude. One shouldn't come to comp.lang.c++ "looking for
something", and then becom irrate when the respondant doesn't "succeed".

Well you can try, but you'll receive abusive responses from me.

Don't get heated up for that, for fsck's sake.

A Google for "C macro functions" will yield dozens of tutorials.

Here's a clue:

#define DECADD(a) int a; global_chain.Register(a);

--

Frederick Gotham
Aug 11 '06 #12

P: n/a
Jo**************@gmail.com wrote:
[snip]
Uhm, look, what I'm asking is simple. I want to do this:
Sigh. If it's so simple, perhaps you can explain it more clearly
than "the story of my life, here I am."
int a, b;
list.add(&a); list.add(&b);

with something like this:

declare_and_add(a);
declare_and_add(b);

Now, fill in the blank for 'declare_and_add' (either with a #define,
template, whatever). Don't ask about the particular context -- this is
a totally out of context, _simple_ problem (not that the solution is).
The thing is, it's not "that simple." The context is almost never
trivial in such things.

You *seem* to want to have the address of a variable stored in
a container of some kind, without that variable existing outside
the container. Or you want the declaration of that variable to
happen "invisibly" with no explicit "int a;" kind of statement.
Without some very good reason, each of these is vastly ugly.

Let me throw spackle at this from another angle. Supposing it is
possible to make a macro etc. that does the equivalent of (with
the line MyContainer myList; taken as "read")

int a;
myList.add(&a);

as a single statement, something like this.

DeclareAndAdd(myList,a);

That is, suppose that every time you did DeclareAndAdd(x,y)
that a new variable y of type int got declared, and a pointer to it got
added to the container x. Then suppose that I came along as your
code reviewer. I'd require you to remove this from your code,
and the scribbled comments in the margin of the printout would
be most unkind.

So, unless you can give some explanation of what's wrong with
the first version, I've got to start wondering just what it is you
are trying to do. Hence my, and several other people, asking
about context. What you are asking for, without context, looks
pretty silly.
Socks

Aug 11 '06 #13

P: n/a
Jo**************@gmail.com wrote:
Frederick Gotham wrote:
Frederick Gotham posted:
void PrintAllRegistered() const
{
cout << first_int << '\n';
>
for(LinkedPointer<int*const*p = &first_link.pnext; ...

LinkedPointer<intconst *const *p

--

Frederick Gotham


Uhm, look, what I'm asking is simple. I want to do this:

int a, b;
list.add(&a); list.add(&b);

with something like this:

declare_and_add(a);
declare_and_add(b);

Now, fill in the blank for 'declare_and_add' (either with a #define,
template, whatever). Don't ask about the particular context -- this is
a totally out of context, _simple_ problem (not that the solution is).
So do you just want:

#define declare_and_add(v) \
int v ; \
list.add(&v) ;

--
Alan Johnson

Aug 11 '06 #14

P: n/a

Puppet_Sock wrote:
Jo**************@gmail.com wrote:
[snip]
Uhm, look, what I'm asking is simple. I want to do this:

Sigh. If it's so simple, perhaps you can explain it more clearly
than "the story of my life, here I am."
int a, b;
list.add(&a); list.add(&b);

with something like this:

declare_and_add(a);
declare_and_add(b);

Now, fill in the blank for 'declare_and_add' (either with a #define,
template, whatever). Don't ask about the particular context -- this is
a totally out of context, _simple_ problem (not that the solution is).

The thing is, it's not "that simple." The context is almost never
trivial in such things.

You *seem* to want to have the address of a variable stored in
a container of some kind, without that variable existing outside
the container. Or you want the declaration of that variable to
happen "invisibly" with no explicit "int a;" kind of statement.
Without some very good reason, each of these is vastly ugly.

Let me throw spackle at this from another angle. Supposing it is
possible to make a macro etc. that does the equivalent of (with
the line MyContainer myList; taken as "read")

int a;
myList.add(&a);

as a single statement, something like this.

DeclareAndAdd(myList,a);

That is, suppose that every time you did DeclareAndAdd(x,y)
that a new variable y of type int got declared, and a pointer to it got
added to the container x. Then suppose that I came along as your
code reviewer. I'd require you to remove this from your code,
and the scribbled comments in the margin of the printout would
be most unkind.

So, unless you can give some explanation of what's wrong with
the first version, I've got to start wondering just what it is you
are trying to do. Hence my, and several other people, asking
about context. What you are asking for, without context, looks
pretty silly.
Socks
Ok, the list example was bad. Referring back to my first post, I want
to declare a variable and append it in an array at the same time, at
compile time.

int foo;
int bar;
int * array = {&foo, &bar};

A typical context would be that you're sending a message with different
fields. Having int * array lets you just do:

send(array,2);

Now say you want to add a field, you have to do:
int bob;

_and_ modify int * array to have &bob at the end. What I want is
somthing like:

int * array;
FIELD(foo);
FIELD(bar);
FIELD(bob);

where FIELD would be something like:
#define FIELD(x) int x; array = {array, &x};

Yes, I know, I'm not in Matlab and that doesn't compile. Now you're
going to say "Well, just use a union!":

union
{
struct { int foo, bar, bob; }
int data[3];

};

So I just have to mention 'bob' one time (as I want). But actually I'm
dealing with classes (and not int's) and I have clases of different
sizes, so data won't work (and I don't even know if you can put classes
in a union. anyways).

The whole point is saving the trouble of having to go and change the
'array' by hand; yeah I know I'm pretty lazy, but I'm just asking if
there's a way around it.

That's all.

Aug 11 '06 #15

P: n/a
Jonathan Fillion wrote:
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it.
There is.
I want to declare variables while adding them to a list
simultaneously.
Right, and I want to answer you without editorial comment on whether
this toy example is correct or something similarly silly. ;)
int a, b, c;
int* list[] = {&a, &b, &c};

But I only want to declare 'a', 'b' and 'c' once (they're declared 2
time here, one for the instance and one for the pointer in the list).
Now I figure it's possible to do that using a macro, but I just can't
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();
Doing something approaching that is possible. Metamacros are your
friend. ;)

Macros may be evil, but the fact is that duplicating knowledge is even
more evil. Does this example give you a pointer in the right
direction?

#define FRUIT_ENUM(e_) \
e_(Apple) \
e_(Banana) \
e_(Orange)

#define ENUM_AS_ENUM(x_) x_,
#define ENUM_AS_STRING(x_) #x_,

enum Fruit
{
FRUIT_ENUM(ENUM_AS_ENUM)
FruitCount
};

const char* g_fruitName[] =
{
FRUIT_ENUM(ENUM_AS_STRING)
};

int main()
{
for (int i = 0; i < FruitCount; ++i)
{
Fruit fruit = Fruit(i);
const char* name = g_fruitName[i];
}
}

If that doesn't help... (Change names as you see appropriate.)

#define LIST_MEMBERS(e_) \
e_(int, a) \
e_(int, b) \
e_(int, c)

// I would probably remove the 'int's from above and just
// push it into the macro here, since it's never changing.
// However they stay here for an idea about how to deal with
// parameters that aren't necessarily required for the given
// expansion. (e_ means 'expansion' or something similar.)
#define LIST_DECLARE(ty_, x_) ty_ x_;
#define LIST_INTO_LIST(ty_, x_) x_,

LIST_MEMBERS(DECLARE_LIST)
int* list[] = { LIST_MEMBERS(LIST_INTO_LIST) };

g'luck,
Aug 11 '06 #16

P: n/a
Jonathan Fillion wrote:
Uhm, look, what I'm asking is simple. I want to do this:

int a, b;
list.add(&a); list.add(&b);
This is considerably different from your original post, fwiw.
Whichever you prefer, however, should be trivial to adapt with the
example I gave earlier.
(not that the solution is).
The solution is simple, if you're not on a holy crusade against
features of the language.

To defend the macros, the last time I did something like this was akin
to the fruit example, but for specifying command-line parameters. A
list was built with all of the command-line parameters, and the
instantiations built the globals into which the info got dropped as
well as the list of strings to compare against, as well as all of the
externs in the commandline.h header. It's just too much duplication
of trivial code, which is prone to error, to do any other way.

there's the story according to me,
Aug 11 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.