473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

macros and functions

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
Nov 14 '05 #1
8 1268
ev******@cnam.fr (Evangelista Sami) wrote in
news:5f**************************@posting.google.c 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 ->
--
Nov 14 '05 #2
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
rg********@pobox.com
Remove the 'g' from my address to send me mail.

Nov 14 '05 #3
Mark A. Odell wrote:
ev******@cnam.fr (Evangelista Sami) wrote in
news:5f**************************@posting.google.c 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!"
Nov 14 '05 #4
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!"
Nov 14 '05 #5
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) );

--
Er*********@sun.com
Nov 14 '05 #6
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
Nov 14 '05 #7
On 13 Feb 2004 17:56:41 GMT, "Mark A. Odell" <no****@embeddedfw.com>
wrote:
ev******@cnam.fr (Evangelista Sami) wrote in
news:5f**************************@posting.google. 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
re************************@att.net
Nov 14 '05 #8
Artie Gold <ar*******@austin.rr.com> wrote in
news:c0*************@ID-219787.news.uni-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 ->
--
Nov 14 '05 #9

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

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
37
by: seberino | last post by:
I've been reading the beloved Paul Graham's "Hackers and Painters". He claims he developed a web app at light speed using Lisp and lots of macros. It got me curious if Lisp is inherently faster...
11
by: Ben Hetland | last post by:
....in certain cituations they can be useful if not for anything else, then at least for saving a lot of repetetetetetitititive typing. :-) Beyond the point of "do something better instead", I'm...
12
by: David Powell | last post by:
Because my work area won't have an Access programmer next year, I've been asked to rebuild their coded application as a set of modular tools. The idea is that a non-programmer will be able to...
3
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v +=...
47
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant...
3
by: fiNAL.Y | last post by:
Many C++ masters recommend us not to use Macros in C++ but to use inline functions and const . But I think that Macros can do something inline functions and const cannot do. It can "extend" the...
3
by: Michael Brennan | last post by:
Hi, does the standard say if the isalpha, isdigit, etc. functions in ctype.h are functions or macros? My old pre-ansi book says they are macros, but I've seen man pages that say they are...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.