Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > macros and functions

Reply
Thread Tools

macros and functions

 
 
Evangelista Sami
Guest
Posts: n/a
 
      02-13-2004
hello everybody

let's take this struct

typedef struct {
int i;
int j;
} my_struct;

is there a way to transform this function :

my_struct new_my_struct
(int i,
int j)
{
my_struct res;
res.i = i;
res.j = j;
return res;
}

into a single macro using the awful permissions of the C syntax?

Sami Evangelista
 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      02-13-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Evangelista Sami) wrote in
news:(E-Mail Removed) om:

> let's take this struct
>
> typedef struct {
> int i;
> int j;
> } my_struct;
>
> is there a way to transform this function :
>
> my_struct new_my_struct
> (int i,
> int j)
> {
> my_struct res;
> res.i = i;
> res.j = j;
> return res; <-- as soon as you return 'res' disappears!
> }
>
> into a single macro using the awful permissions of the C syntax?


Awful?! Well I never!

--
- Mark ->
--
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-13-2004
Evangelista Sami wrote:
>
> hello everybody
>
> let's take this struct
>
> typedef struct {
> int i;
> int j;
> } my_struct;
>
> is there a way to transform this function :
>
> my_struct new_my_struct
> (int i,
> int j)
> {
> my_struct res;
> res.i = i;
> res.j = j;
> return res;
> }
>
> into a single macro using the awful permissions of the C syntax?


If you intend to use the macro for initialization,
the macro is simple:

#define new_my_struct(x,jy) { (x), (y) }

Now you can write

my_struct thing = new_my_struct(1, 42);

which will expand to

my_struct thing = { (1), (42) };

If you want to use the macro for ordinary assignments,
function calls, and so on, you're out of luck unless you have
a compiler that supports the latest "C99" Standard. If you
do, you can write

#define new_my_struct(x,y) (my_struct){ .i=(x), .j=(y) }

and then

my_struct thing;
...
thing = new_my_struct(1,2);
...
thing = new_my_struct(3,4);
...
some_function( new_my_struct(17,42) );

--
(E-Mail Removed)
 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      02-13-2004
Evangelista Sami wrote:

> hello everybody
>
> let's take this struct
>
> typedef struct {
> int i;
> int j;
> } my_struct;


> is there a way to transform this function :


> my_struct new_my_struct
> (int i,
> int j)
> {
> my_struct res;
> res.i = i;
> res.j = j;
> return res;
> }


> into a single macro using the awful permissions of the C syntax?


The following seems to work in gcc3.3.1, and I assume in C99, although I
don't have time to look at the standard:

my_struct
new_my_struct(int i, int j)
{
return (my_struct){i, j};
}

I don't really understand why you need this kind of function when you
can just as easily declare

my_struct ms = {i, j};

/david
 
Reply With Quote
 
Russell Hanneken
Guest
Posts: n/a
 
      02-13-2004
Evangelista Sami wrote:
>
> is there a way to transform this function :
>
> my_struct new_my_struct
> (int i,
> int j)
> {
> my_struct res;
> res.i = i;
> res.j = j;
> return res;
> }
>
> into a single macro using the awful permissions of the C syntax?


I don't know what you mean by "awful permissions," but here's my inexpert
(inept?) solution:

#include <stdio.h>

#define NEW_MY_STRUCT(x, a, b) my_struct (x); (x).i = (a); (x).j = (b)

typedef struct {
int i;
int j;
} my_struct;

int main(void)
{
NEW_MY_STRUCT(foo, 1, 2);
printf("foo.i = %d\nfoo.j = %d\n", foo.i, foo.j);
return 0;
}

I've always felt that macros are dangerous and tend to obscure the meaning
of code. It wouldn't surprise me if someone pointed out a case where my
macro did something I didn't intend.

--
Russell Hanneken
(E-Mail Removed)
Remove the 'g' from my address to send me mail.



 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      02-13-2004
Mark A. Odell wrote:
> (E-Mail Removed) (Evangelista Sami) wrote in
> news:(E-Mail Removed) om:
>
>
>>let's take this struct
>>
>>typedef struct {
>> int i;
>> int j;
>>} my_struct;
>>
>>is there a way to transform this function :
>>
>>my_struct new_my_struct
>>(int i,
>> int j)
>>{
>> my_struct res;
>> res.i = i;
>> res.j = j;
>> return res; <-- as soon as you return 'res' disappears!


Erm, no. A copy is returned.
>>}
>>
>>into a single macro using the awful permissions of the C syntax?

>
>

--ag

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      02-13-2004
Evangelista Sami wrote:
> hello everybody
>
> let's take this struct
>
> typedef struct {
> int i;
> int j;
> } my_struct;
>
> is there a way to transform this function :
>
> my_struct new_my_struct
> (int i,
> int j)
> {
> my_struct res;
> res.i = i;
> res.j = j;
> return res;
> }
>
> into a single macro using the awful permissions of the C syntax?
>

What are you trying to accomplish?

I suspect you're asking the wrong question here.

HTH,
--ag
--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      02-13-2004
On 13 Feb 2004 17:56:41 GMT, "Mark A. Odell" <(E-Mail Removed)>
wrote:

>(E-Mail Removed) (Evangelista Sami) wrote in
>news:(E-Mail Removed). com:
>
>> let's take this struct

<snip>
>> res.j = j;
>> return res; <-- as soon as you return 'res' disappears!
>> }

Huh? Evangelista Sami didn't write that. Are you missing an
attribution, or is there a missing end of line above?

Anyway, it's true, but doesn't matter, since the result will either be
assigned to something or ignored.

>>
>> into a single macro using the awful permissions of the C syntax?

>

<snip>
--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      02-13-2004
Artie Gold <(E-Mail Removed)> wrote in
news:c0j4bv$18cndk$(E-Mail Removed)-berlin.de:


>>> my_struct res;
>>> res.i = i;
>>> res.j = j;
>>> return res; <-- as soon as you return 'res' disappears!

>
> Erm, no. A copy is returned.


Ugh. Me and my habit of always passing structs around as pointers. Of
course you are correct, the caller gets a copy not a pointer to memory
that is no longer valid.

--
- Mark ->
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macros-loop? calling macros X times? Andrew Arro C Programming 2 07-24-2004 09:52 AM
Explanation of macros; Haskell macros mike420@ziplip.com Python 80 11-07-2003 02:22 AM
Re: Explanation of macros; Haskell macros Michael T. Babcock Python 0 11-03-2003 01:54 PM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 5 11-01-2003 01:09 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 1 10-07-2003 04:07 PM



Advertisments