micro to declare and initialize a struct pointer does not work, why?

Discussion in 'C Programming' started by KOFKS, Mar 3, 2009.

  1. KOFKS

    KOFKS Guest

    code like that:

    #include <stdlib.h>

    #define decPtr(type, name) do { (type) * (name) = malloc(sizeof
    (type) ); } while (0);

    typedef struct pt
    {
    int x;
    int y;
    } pt;

    int main( int argc, char *argv[])
    {
    decPtr(pt, pt0);
    return 0;
    }

    The compiler complains:
    pt0 is an identifier has not been declared(something means this, I
    interprete it from native language other than English)

    Is my implementation wrong, or this method does not work through?
    Anyone help? Thanks a lot~
     
    KOFKS, Mar 3, 2009
    #1
    1. Advertising

  2. Re: micro to declare and initialize a struct pointer does not work,why?

    KOFKS <> wrote:
    > code like that:
    >
    > #include <stdlib.h>
    >
    > #define decPtr(type, name) do { (type) * (name) = malloc(sizeof
    > (type) ); } while (0);


    You can't declare pointers like...

    (int) *my_int_ptr;

    Even if you remove the parentheses, the do/while block limits
    the scope of the declared identifier. You'll simply have a
    memory leak.

    > typedef struct pt
    > {
    > int x;
    > int y;
    > } pt;
    >
    > int main( int argc, char *argv[])
    > {
    > decPtr(pt, pt0);


    C is not C++. Creating a struct pt does not make pt a type
    synonym for struct pt.

    > return 0;
    > }
    >
    > The compiler complains:
    > pt0 is an identifier has not been declared(something means
    > this, I interprete it from native language other than English)


    It's not a very informative error message. However, the way
    you've written your code isn't that common!

    > Is my implementation wrong, or this method does not work
    > through?


    Even if it worked, why bother using a macro to cloud such a
    simple line of code...?

    struct pt *pt0 = malloc(sizeof *pt0);

    --
    Peter
     
    Peter Nilsson, Mar 3, 2009
    #2
    1. Advertising

  3. KOFKS

    Kaz Kylheku Guest

    Re: micro to declare and initialize a struct pointer does not work,why?

    On 2009-03-03, KOFKS <> wrote:
    > code like that:
    >
    > #include <stdlib.h>
    >
    > #define decPtr(type, name) do { (type) * (name) = malloc(sizeof
    > (type) ); } while (0);


    Although parentheses may be used in the declarator syntax, for instance:

    int (x);

    the same is not true of the list of declaration specifiers. There is no
    provision for parenthesis in their syntax:

    (int) x; /* wrong */

    You are falling victim to macro parenthesitis: surrounding the expansion of
    every macro parameter with parentheses, just in case they are needed to protect
    against some kind of ambiguity, without actually thinking about it. Most C
    newbies get this malady in their first year of C life, thereby becoming immune
    against a second infection.

    > typedef struct pt
    > {
    > int x;
    > int y;
    > } pt;
    >
    > int main( int argc, char *argv[])
    > {
    > decPtr(pt, pt0);
    > return 0;
    > }
    >
    > The compiler complains:
    > pt0 is an identifier has not been declaredsomething means this, I
    > interprete it from native language other than English


    The syntax

    (pt) * (pt0) = malloc(...)

    is not a declaration at all. It's an assignment expression with a cast on the
    left hand side. We can add parentheses to clarify the syntax:

    ((pt) (*(pt0))) = malloc(...)

    It's complete nonsense, which means: dereference the pointer pt0 (which doesn't
    exist), cast the result to type pt, and then treat that as the target of an
    assignment.
     
    Kaz Kylheku, Mar 3, 2009
    #3
  4. KOFKS

    KOFKS Guest

    Re: micro to declare and initialize a struct pointer does not work,why?

    Thanks to all of you for the detailed explaination~
    Get to know the problem.
     
    KOFKS, Mar 3, 2009
    #4
  5. KOFKS

    Guest

    Re: micro to declare and initialize a struct pointer does not work,why?

    On 3 Mar, 04:45, KOFKS <> wrote:

    > code like that:


    no don't!

    > #include <stdlib.h>
    >
    > #define decPtr(type, name) do { (type) * (name) = malloc(sizeof
    > (type) ); } while (0);


    most of your subsequent problems could have been avoided by *not*
    using
    a macro in the first place.
     
    , Mar 3, 2009
    #5
  6. KOFKS

    Guest

    Re: micro to declare and initialize a struct pointer does not work,why?

    On 3 Mar, 08:15, wrote:
    > On 3 Mar, 04:45, KOFKS <> wrote:
    >
    > > code like that:

    >
    > no don't!
    >
    > > #include <stdlib.h>

    >
    > > #define decPtr(type, name) do { (type) * (name) = malloc(sizeof
    > > (type) ); } while (0);

    >
    > most of your subsequent problems could have been avoided by *not*
    > using a macro in the first place.


    Oh, and the subject line reads:
    "micro to declare and initialize a struct pointer does not work, why?"
    that should be "macro" not "micro"
     
    , Mar 4, 2009
    #6
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,261
    Smokey Grindel
    Dec 2, 2006
  2. beetle
    Replies:
    2
    Views:
    972
    beetle
    Jan 25, 2005
  3. Zero
    Replies:
    16
    Views:
    686
    Barry Schwarz
    Nov 19, 2005
  4. Ehud Shapira
    Replies:
    20
    Views:
    1,185
    Ehud Shapira
    Jun 30, 2007
  5. xmllmx
    Replies:
    4
    Views:
    255
    Zhihao Yuan
    Dec 17, 2012
Loading...

Share This Page