macros and functions

Discussion in 'C Programming' started by Evangelista Sami, Feb 13, 2004.

  1. 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
     
    Evangelista Sami, Feb 13, 2004
    #1
    1. Advertising

  2. (Evangelista Sami) wrote in
    news::

    > 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 ->
    --
     
    Mark A. Odell, Feb 13, 2004
    #2
    1. Advertising

  3. Evangelista Sami

    Eric Sosman Guest

    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) );

    --
     
    Eric Sosman, Feb 13, 2004
    #3
  4. Evangelista Sami

    David Rubin Guest

    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
     
    David Rubin, Feb 13, 2004
    #4
  5. 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

    Remove the 'g' from my address to send me mail.
     
    Russell Hanneken, Feb 13, 2004
    #5
  6. Evangelista Sami

    Artie Gold Guest

    Mark A. Odell wrote:
    > (Evangelista Sami) wrote in
    > news::
    >
    >
    >>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!"
     
    Artie Gold, Feb 13, 2004
    #6
  7. Evangelista Sami

    Artie Gold Guest

    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!"
     
    Artie Gold, Feb 13, 2004
    #7
  8. Evangelista Sami

    Alan Balmer Guest

    On 13 Feb 2004 17:56:41 GMT, "Mark A. Odell" <>
    wrote:

    > (Evangelista Sami) wrote in
    >news::
    >
    >> 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
     
    Alan Balmer, Feb 13, 2004
    #8
  9. Artie Gold <> wrote in
    news:c0j4bv$18cndk$-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 ->
    --
     
    Mark A. Odell, Feb 13, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    80
    Views:
    2,531
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    495
    Marco Antoniotti
    Oct 7, 2003
  3. Replies:
    5
    Views:
    515
  4. Michael T. Babcock

    Re: Explanation of macros; Haskell macros

    Michael T. Babcock, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    543
    Michael T. Babcock
    Nov 3, 2003
  5. Andrew Arro

    macros-loop? calling macros X times?

    Andrew Arro, Jul 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    520
    S.Tobias
    Jul 24, 2004
Loading...

Share This Page