Hello everyone,


I can not understand how the following code works and assign 100 to counter
variable?

[Code]
void Foo (int* input)
{
*input = 100;

return;
}

#define GETFOO (Foo( &counter ), counter)

int counter;

int main()
{
GETFOO;
return 0;
}
[/Code]


thanks in advance,
George

Re: how the macro works by Vladimir

Vladimir
Wed Dec 12 03:40:14 PST 2007

In the function Foo() the statement return is obsolete. So the valid code of
the function looks like

void Foo (int* input)
{
*input = 100;
}

The macro GETFOO uses operator ',' (comma). For the comma operator the last
value in the list is the value of the operator in whole.
for example, for

int i = ( 2, 3);

i will be set to 3.

It is better to rewrite the function Foo() as

int Foo( int & input )
{
input = 100;
return ( input );
}

Vladimir Grigoriev


"George" <George@discussions.microsoft.com> wrote in message
news:AD5B3494-BD1F-4520-AF76-9F708CE66E36@microsoft.com...
> Hello everyone,
>
>
> I can not understand how the following code works and assign 100 to
> counter
> variable?
>
> [Code]
> void Foo (int* input)
> {
> *input = 100;
>
> return;
> }
>
> #define GETFOO (Foo( &counter ), counter)
>
> int counter;
>
> int main()
> {
> GETFOO;
> return 0;
> }
> [/Code]
>
>
> thanks in advance,
> George



Re: how the macro works by George

George
Wed Dec 12 03:49:00 PST 2007

Thanks Vladimir,


I do not understand how this statement works,

(Foo( &counter ), counter);

I have tested that the simple statement (without comment) also works. What
is the benefit and function to add , and counter in macro definition?

// #define GETFOO Foo((&counter), counter)
#define GETFOO Foo(&counter)


regards,
George

"Vladimir Grigoriev" wrote:

> In the function Foo() the statement return is obsolete. So the valid code of
> the function looks like
>
> void Foo (int* input)
> {
> *input = 100;
> }
>
> The macro GETFOO uses operator ',' (comma). For the comma operator the last
> value in the list is the value of the operator in whole.
> for example, for
>
> int i = ( 2, 3);
>
> i will be set to 3.
>
> It is better to rewrite the function Foo() as
>
> int Foo( int & input )
> {
> input = 100;
> return ( input );
> }
>
> Vladimir Grigoriev
>
>
> "George" <George@discussions.microsoft.com> wrote in message
> news:AD5B3494-BD1F-4520-AF76-9F708CE66E36@microsoft.com...
> > Hello everyone,
> >
> >
> > I can not understand how the following code works and assign 100 to
> > counter
> > variable?
> >
> > [Code]
> > void Foo (int* input)
> > {
> > *input = 100;
> >
> > return;
> > }
> >
> > #define GETFOO (Foo( &counter ), counter)
> >
> > int counter;
> >
> > int main()
> > {
> > GETFOO;
> > return 0;
> > }
> > [/Code]
> >
> >
> > thanks in advance,
> > George
>
>
>

Re: how the macro works by Vladimir

Vladimir
Wed Dec 12 04:10:36 PST 2007

The original fucntion returns nothing (void). The macro helps after calling
the function also to get the result value of counter.
For example

// #define GETFOO ( Foo(&counter), counter)

std::cout << "GETFOO = " << GETFOO << std::endl;

Vladimir Grigoriev



"George" <George@discussions.microsoft.com> wrote in message
news:CC8911F2-CDC3-41C3-B26D-1B4B2A2717CC@microsoft.com...
> Thanks Vladimir,
>
>
> I do not understand how this statement works,
>
> (Foo( &counter ), counter);
>
> I have tested that the simple statement (without comment) also works. What
> is the benefit and function to add , and counter in macro definition?
>
> // #define GETFOO Foo((&counter), counter)
> #define GETFOO Foo(&counter)
>
>
> regards,
> George
>
> "Vladimir Grigoriev" wrote:
>
>> In the function Foo() the statement return is obsolete. So the valid code
>> of
>> the function looks like
>>
>> void Foo (int* input)
>> {
>> *input = 100;
>> }
>>
>> The macro GETFOO uses operator ',' (comma). For the comma operator the
>> last
>> value in the list is the value of the operator in whole.
>> for example, for
>>
>> int i = ( 2, 3);
>>
>> i will be set to 3.
>>
>> It is better to rewrite the function Foo() as
>>
>> int Foo( int & input )
>> {
>> input = 100;
>> return ( input );
>> }
>>
>> Vladimir Grigoriev
>>
>>
>> "George" <George@discussions.microsoft.com> wrote in message
>> news:AD5B3494-BD1F-4520-AF76-9F708CE66E36@microsoft.com...
>> > Hello everyone,
>> >
>> >
>> > I can not understand how the following code works and assign 100 to
>> > counter
>> > variable?
>> >
>> > [Code]
>> > void Foo (int* input)
>> > {
>> > *input = 100;
>> >
>> > return;
>> > }
>> >
>> > #define GETFOO (Foo( &counter ), counter)
>> >
>> > int counter;
>> >
>> > int main()
>> > {
>> > GETFOO;
>> > return 0;
>> > }
>> > [/Code]
>> >
>> >
>> > thanks in advance,
>> > George
>>
>>
>>



Re: comma operator (was: how the macro works) by Ulrich

Ulrich
Wed Dec 12 04:09:21 PST 2007

George top-posted:
> I do not understand how this statement works,
>
> (Foo( &counter ), counter);
>
> I have tested that the simple statement (without comment) also works.

Which comment?

> What is the benefit and function to add , and counter in macro definition?

Using that, the expression evaluates to the value of 'counter'.

Please read and understand this:

>> The macro GETFOO uses operator ',' (comma). For the comma operator the
>> last value in the list is the value of the operator in whole.

Or maybe the also conveniently added examples:

>> int i = ( 2, 3);
>>
>> i will be set to 3.

Also, please read a bit about behaviour on the Usenet (hint: TOFU,
top-posting, homework etc).

Uli


Re: how the macro works by George

George
Wed Dec 12 04:58:00 PST 2007

Thanks Vladimir,


My question is,

I think using

#define GETFOO (Foo( &counter ), counter)

is the same as

#define GETFOO Foo( &counter )

So, no benefits of using , operator, right?


regards,
George

"Vladimir Grigoriev" wrote:

> The original fucntion returns nothing (void). The macro helps after calling
> the function also to get the result value of counter.
> For example
>
> // #define GETFOO ( Foo(&counter), counter)
>
> std::cout << "GETFOO = " << GETFOO << std::endl;
>
> Vladimir Grigoriev
>
>
>
> "George" <George@discussions.microsoft.com> wrote in message
> news:CC8911F2-CDC3-41C3-B26D-1B4B2A2717CC@microsoft.com...
> > Thanks Vladimir,
> >
> >
> > I do not understand how this statement works,
> >
> > (Foo( &counter ), counter);
> >
> > I have tested that the simple statement (without comment) also works. What
> > is the benefit and function to add , and counter in macro definition?
> >
> > // #define GETFOO Foo((&counter), counter)
> > #define GETFOO Foo(&counter)
> >
> >
> > regards,
> > George
> >
> > "Vladimir Grigoriev" wrote:
> >
> >> In the function Foo() the statement return is obsolete. So the valid code
> >> of
> >> the function looks like
> >>
> >> void Foo (int* input)
> >> {
> >> *input = 100;
> >> }
> >>
> >> The macro GETFOO uses operator ',' (comma). For the comma operator the
> >> last
> >> value in the list is the value of the operator in whole.
> >> for example, for
> >>
> >> int i = ( 2, 3);
> >>
> >> i will be set to 3.
> >>
> >> It is better to rewrite the function Foo() as
> >>
> >> int Foo( int & input )
> >> {
> >> input = 100;
> >> return ( input );
> >> }
> >>
> >> Vladimir Grigoriev
> >>
> >>
> >> "George" <George@discussions.microsoft.com> wrote in message
> >> news:AD5B3494-BD1F-4520-AF76-9F708CE66E36@microsoft.com...
> >> > Hello everyone,
> >> >
> >> >
> >> > I can not understand how the following code works and assign 100 to
> >> > counter
> >> > variable?
> >> >
> >> > [Code]
> >> > void Foo (int* input)
> >> > {
> >> > *input = 100;
> >> >
> >> > return;
> >> > }
> >> >
> >> > #define GETFOO (Foo( &counter ), counter)
> >> >
> >> > int counter;
> >> >
> >> > int main()
> >> > {
> >> > GETFOO;
> >> > return 0;
> >> > }
> >> > [/Code]
> >> >
> >> >
> >> > thanks in advance,
> >> > George
> >>
> >>
> >>
>
>
>

Re: comma operator (was: how the macro works) by George

George
Wed Dec 12 04:58:01 PST 2007

Thanks Uli,


My question is,

I think using

#define GETFOO (Foo( &counter ), counter)

is the same as

#define GETFOO Foo( &counter )

So, no benefits of using , operator, right?


regards,
George

"Ulrich Eckhardt" wrote:

> George top-posted:
> > I do not understand how this statement works,
> >
> > (Foo( &counter ), counter);
> >
> > I have tested that the simple statement (without comment) also works.
>
> Which comment?
>
> > What is the benefit and function to add , and counter in macro definition?
>
> Using that, the expression evaluates to the value of 'counter'.
>
> Please read and understand this:
>
> >> The macro GETFOO uses operator ',' (comma). For the comma operator the
> >> last value in the list is the value of the operator in whole.
>
> Or maybe the also conveniently added examples:
>
> >> int i = ( 2, 3);
> >>
> >> i will be set to 3.
>
> Also, please read a bit about behaviour on the Usenet (hint: TOFU,
> top-posting, homework etc).
>
> Uli
>
>

Re: how the macro works by Vladimir

Vladimir
Wed Dec 12 05:18:38 PST 2007


No, there is a difference between the two macros. By using the first macro
you can get a value of counter. The second macro is fully equivalent to
calling the Foo() function which is declared originally as void.

Vladimir Grigoriev

"George" <George@discussions.microsoft.com> wrote in message
news:FC3FA56D-53E0-4CE1-9527-095311DF1B53@microsoft.com...
> Thanks Vladimir,
>
>
> My question is,
>
> I think using
>
> #define GETFOO (Foo( &counter ), counter)
>
> is the same as
>
> #define GETFOO Foo( &counter )
>
> So, no benefits of using , operator, right?
>
>
> regards,
> George
>



Re: how the macro works by George

George
Wed Dec 12 05:29:00 PST 2007

Thanks Vladimir,


My question is answered! It is my mistake to misunderstand this macro.


regards,
George

"Vladimir Grigoriev" wrote:

>
> No, there is a difference between the two macros. By using the first macro
> you can get a value of counter. The second macro is fully equivalent to
> calling the Foo() function which is declared originally as void.
>
> Vladimir Grigoriev
>
> "George" <George@discussions.microsoft.com> wrote in message
> news:FC3FA56D-53E0-4CE1-9527-095311DF1B53@microsoft.com...
> > Thanks Vladimir,
> >
> >
> > My question is,
> >
> > I think using
> >
> > #define GETFOO (Foo( &counter ), counter)
> >
> > is the same as
> >
> > #define GETFOO Foo( &counter )
> >
> > So, no benefits of using , operator, right?
> >
> >
> > regards,
> > George
> >
>
>
>

Re: how the macro works by David

David
Wed Dec 12 05:39:18 PST 2007

George wrote:
> Thanks Vladimir,
>
>
> I do not understand how this statement works,
>
> (Foo( &counter ), counter);
>
> I have tested that the simple statement (without comment) also works. What
> is the benefit and function to add , and counter in macro definition?
>
> // #define GETFOO Foo((&counter), counter)
> #define GETFOO Foo(&counter)

George:

Do you read the replies that you get? Alexander has given you the answer
to this question, yet you reply as if you have not read it.

Your function Foo() does not return a value, but the macro GETFOO does
(due to the comma operator). Your example does not use the return value,
but you could have done

int counter;

int main()
{
return GETFOO;
}

In this case, the code would not compile if you had just done

#define GETFOO Foo(&counter)

because this GETFOO returns void.

--
David Wilkinson
Visual C++ MVP

Re: how the macro works by David

David
Wed Dec 12 06:26:09 PST 2007

David Wilkinson wrote:
> Do you read the replies that you get? Alexander has given you the answer
> to this question, yet you reply as if you have not read it.

Correction: Vladimir. Apologies.

--
David Wilkinson
Visual C++ MVP

Re: how the macro works by George

George
Wed Dec 12 06:28:01 PST 2007

Thanks David,


My question is answered. Thank again for your and other people's help. It is
my mistake to misunderstand your answer.


regards,
George

"David Wilkinson" wrote:

> George wrote:
> > Thanks Vladimir,
> >
> >
> > I do not understand how this statement works,
> >
> > (Foo( &counter ), counter);
> >
> > I have tested that the simple statement (without comment) also works. What
> > is the benefit and function to add , and counter in macro definition?
> >
> > // #define GETFOO Foo((&counter), counter)
> > #define GETFOO Foo(&counter)
>
> George:
>
> Do you read the replies that you get? Alexander has given you the answer
> to this question, yet you reply as if you have not read it.
>
> Your function Foo() does not return a value, but the macro GETFOO does
> (due to the comma operator). Your example does not use the return value,
> but you could have done
>
> int counter;
>
> int main()
> {
> return GETFOO;
> }
>
> In this case, the code would not compile if you had just done
>
> #define GETFOO Foo(&counter)
>
> because this GETFOO returns void.
>
> --
> David Wilkinson
> Visual C++ MVP
>

Re: how the macro works by Duane

Duane
Wed Dec 12 16:10:18 PST 2007


"George" <George@discussions.microsoft.com> wrote in message
news:98DDEADC-CE21-4B36-A562-A6DE35A755E2@microsoft.com...
> Thanks David,
>
>
> My question is answered. Thank again for your and other people's help. It
> is
> my mistake to misunderstand your answer.

While you're talking about macros, please google for Stroustrup and "macros
are evil"

Seems sort of topical given your misunderstanding. Sort of indicative
of macros in general IMO.



Re: how the macro works by George

George
Wed Dec 12 18:54:00 PST 2007

Thanks Duane,


What is *indicative of macros*?


regards,
George

"Duane Hebert" wrote:

>
> "George" <George@discussions.microsoft.com> wrote in message
> news:98DDEADC-CE21-4B36-A562-A6DE35A755E2@microsoft.com...
> > Thanks David,
> >
> >
> > My question is answered. Thank again for your and other people's help. It
> > is
> > my mistake to misunderstand your answer.
>
> While you're talking about macros, please google for Stroustrup and "macros
> are evil"
>
> Seems sort of topical given your misunderstanding. Sort of indicative
> of macros in general IMO.
>
>
>

Re: comma operator (was: how the macro works) by Tim

Tim
Wed Dec 12 23:04:44 PST 2007

George <George@discussions.microsoft.com> wrote:
>
>My question is,
>
>I think using
>
>#define GETFOO (Foo( &counter ), counter)
>
>is the same as
>
>#define GETFOO Foo( &counter )
>
>So, no benefits of using , operator, right?

No, you clearly have not been reading Uli's very helpful posts.

The comma operator evaluates the expression on both sides, and returns the
value of the expression on the RIGHT.

So, for example:

int i, j, k, l, m;
m = (i = 2, j = 3, k = 4, l = 5, 6);

After that statement, i==2, j==3, k==4, l==5, and m==6. m==6 because the
comma operator returns the result on the RIGHT.

So, in this:
#define GETFOO (Foo( &counter ), counter)

The "return" GETFOO is counter, because that's the value on the RIGHT.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: comma operator (was: how the macro works) by George

George
Wed Dec 12 23:19:00 PST 2007

Thanks for your clarification, Tim!


My question is answered.


regards,
George

"Tim Roberts" wrote:

> George <George@discussions.microsoft.com> wrote:
> >
> >My question is,
> >
> >I think using
> >
> >#define GETFOO (Foo( &counter ), counter)
> >
> >is the same as
> >
> >#define GETFOO Foo( &counter )
> >
> >So, no benefits of using , operator, right?
>
> No, you clearly have not been reading Uli's very helpful posts.
>
> The comma operator evaluates the expression on both sides, and returns the
> value of the expression on the RIGHT.
>
> So, for example:
>
> int i, j, k, l, m;
> m = (i = 2, j = 3, k = 4, l = 5, 6);
>
> After that statement, i==2, j==3, k==4, l==5, and m==6. m==6 because the
> comma operator returns the result on the RIGHT.
>
> So, in this:
> #define GETFOO (Foo( &counter ), counter)
>
> The "return" GETFOO is counter, because that's the value on the RIGHT.
> --
> Tim Roberts, timr@probo.com
> Providenza & Boekelheide, Inc.
>

Re: how the macro works by Duane

Duane
Thu Dec 13 03:10:28 PST 2007


"George" <George@discussions.microsoft.com> wrote in message
news:2AA2899C-0E4A-4D12-8DF4-15CB8F9922E3@microsoft.com...
> Thanks Duane,
>
>
> What is *indicative of macros*?

That after using a macro to replace a function,
you don't see how it works any longer.

A lot of people, seemingly Stroustrup included,
think this is a bad idea. Certainly at our shop
a macro like that would not meet our coding
standards.



Re: how the macro works by George

George
Thu Dec 13 03:20:00 PST 2007

Thanks for your clarification, Duane!


regards,
George

"Duane Hebert" wrote:

>
> "George" <George@discussions.microsoft.com> wrote in message
> news:2AA2899C-0E4A-4D12-8DF4-15CB8F9922E3@microsoft.com...
> > Thanks Duane,
> >
> >
> > What is *indicative of macros*?
>
> That after using a macro to replace a function,
> you don't see how it works any longer.
>
> A lot of people, seemingly Stroustrup included,
> think this is a bad idea. Certainly at our shop
> a macro like that would not meet our coding
> standards.
>
>
>