macros and functions

E

Evangelista Sami

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
 
M

Mark A. Odell

(e-mail address removed) (Evangelista Sami) wrote in

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!
 
E

Eric Sosman

Evangelista said:
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) );
 
D

David Rubin

Evangelista said:
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
 
R

Russell Hanneken

Evangelista said:
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.
 
A

Artie Gold

Evangelista said:
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
 
A

Alan Balmer

(e-mail address removed) (Evangelista Sami) wrote in
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.
<snip>
 
M

Mark A. Odell

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top