473,661 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Named variable in a list?

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_instanc es();
declare_list_po inters();

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
16 1870
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_instanc es();
declare_list_po inters();

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
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_instanc es();
declare_list_po inters();

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
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_instanc es();
declare_list_po inters();

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
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<i ntfirst_link;

public:

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

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

LinkedPointer<i nt**p;

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

*p = new LinkedPointer<i nt>;

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

return *this;
}

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

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

Chain global_chain;

void SomeFunc()
{
int static i = 67;

global_chain.Re gister(i);
}

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

global_chain.Re gister(a).Regis ter(b).Register (c);

SomeFunc();

int &d = *new int(13);

global_chain.Re gister(d);

global_chain.Pr intAllRegistere d();

delete &d;
}

--

Frederick Gotham
Aug 11 '06 #5
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_instanc es();
declare_list_po inters();
>
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
Frederick Gotham posted:
void PrintAllRegiste red() const
{
cout << first_int << '\n';

for(LinkedPoint er<int*const*p = &first_link.pne xt; ...

LinkedPointer<i ntconst *const *p

--

Frederick Gotham
Aug 11 '06 #7
Frederick Gotham wrote:
Frederick Gotham posted:
void PrintAllRegiste red() const
{
cout << first_int << '\n';

for(LinkedPoint er<int*const*p = &first_link.pne xt; ...


LinkedPointer<i ntconst *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_ad d' (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
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_ad d'

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<Jonath anFillion>;

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
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_ad d'


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<Jonath anFillion>;

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4550
by: Carsten Gehling | last post by:
Figured out myself - I just removed the line with "from url import Url" -it doesn't seem to be used anywhere ;-) - Carsten > -----Oprindelig meddelelse----- > Fra: python-list-admin@python.org > På vegne af Carsten Gehling > Sendt: 16. juli 2003 16:24
5
2015
by: Magnus Lyck? | last post by:
Something really strange is happening to me (sometimes). I'm using Python 2.3.2 on NT 4.0 as well as win32all-157, adodbapi and db_row. During a recursive call to a method, it seems Python messes up its variable bindings once in a while. Suddenly, one of several local variables gets rebound to the object it was bound to one step up in the recursion. The relevant part of the code looks like this with print statements
0
5285
by: Matt W | last post by:
Hi all, I just noticed this in the manual yesterday: http://www.mysql.com/doc/en/Windows_running.html "MySQL supports TCP/IP on all Windows platforms. The mysqld-nt and mysql-max-nt servers support named pipes on NT, 2000, and XP. The default is to use TCP/IP regardless of the platform, because named pipes are actually *slower* than TCP/IP ..."
2
6020
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x < 20; x++) { document.all('mySpan'+x).style.display = "inline"; } But I don´t know how many SPAN elements there are, so I need to set x to a
18
3447
by: Peter Hardy | last post by:
Hi Guys, Can I use named parameters in C# Methods. i.e doFoo(fname="do", lname="Foo"); Cheers, Pete
8
3036
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of this, if the code is jitted the "push xyz" instructions of the actual default values can be inserted. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself why one to...
8
1513
by: Paddy | last post by:
Proposal: Named RE variables ====================== The problem I have is that I am writing a 'good-enough' verilog tag extractor as a long regular expression (with the 'x' flag for readability), and find myself both 1) Repeating sections of the RE, and 2) Wanting to add '(?P<some_clarifier>...) ' around sections because I know what the section does but don't really want the group.
2
3322
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this simple example, def fun1(**kwargs): print kwargs.keys() def fun2(**kwargs):
0
3728
by: bfrank1972 | last post by:
I want to be able to get a list of all custom named fields in an Excel worksheet, but I am having trouble with this. In the code below, access to a field that I named "DEALCODE" works fine - I get the value. When I try to get a collection of named cells in the loop below, I come up with only one entry: 'Portfolio Company Information'!Print_Area The part before the ! is obviously my sheet name, but I have a whole slew of custom named cells in...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.