Preprocessor errors?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sensei

    #1

    Preprocessor errors?

    Hi!

    I'm getting some weird results from a (probably) valid C expression
    with some #define... I'm really puzzled.

    A sample code is this (just the variables and defines, not actual code):

    #define themacro(p, i) (p)[(i)]

    int *ptr, *anotherptr, counter;

    themacro(ptr, counter)=anothe rptr=MEMORY_ALL OCATION(...);

    Where memory_allocati on is a function which mallocs and reallocs
    memory. I get a weird null pointer somewhere, and just writing:

    anotherptr=MEMO RY_ALLOCATION(. ..);
    themacro(ptr, counter)=anothe rptr;

    solves the problem. Am I doing something out of standard?

    --
    Sensei <senseiwa@mac.c om>

    Part of the inhumanity of the computer is that, once it is competently
    programmed and working smoothly, it is completely honest. (Isaac Asimov)

  • Artie Gold

    #2
    Re: Preprocessor errors?

    Sensei wrote:[color=blue]
    > Hi!
    >
    > I'm getting some weird results from a (probably) valid C expression with
    > some #define... I'm really puzzled.
    >
    > A sample code is this (just the variables and defines, not actual code):
    >
    > #define themacro(p, i) (p)[(i)]
    >
    > int *ptr, *anotherptr, counter;
    >
    > themacro(ptr, counter)=anothe rptr=MEMORY_ALL OCATION(...);
    >
    > Where memory_allocati on is a function which mallocs and reallocs memory.
    > I get a weird null pointer somewhere, and just writing:
    >
    > anotherptr=MEMO RY_ALLOCATION(. ..);
    > themacro(ptr, counter)=anothe rptr;
    >
    > solves the problem. Am I doing something out of standard?
    >[/color]
    Show a compilable snippet that exhibits the problem. *Real code*. Cut
    and paste.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.

    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"

    Comment

    • Christopher Benson-Manica

      #3
      Re: Preprocessor errors?

      Sensei <senseiwa@mac.c om> wrote:
      [color=blue]
      > themacro(ptr, counter)=anothe rptr=MEMORY_ALL OCATION(...);[/color]
      [color=blue]
      > anotherptr=MEMO RY_ALLOCATION(. ..);
      > themacro(ptr, counter)=anothe rptr;[/color]
      [color=blue]
      > solves the problem. Am I doing something out of standard?[/color]

      Possibly; I'm not much of a guru to tell you. If you haven't
      already, however, I would investigate your platform's documentation
      for how to invoke only the preprocessor on your code (gcc -E will do
      it, for example) to make sure the preprocessed line looks the way you
      expect it to.

      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Mark McIntyre

        #4
        Re: Preprocessor errors?

        On Wed, 14 Dec 2005 21:02:42 +0100, in comp.lang.c , Sensei
        <senseiwa@mac.c om> wrote:
        [color=blue]
        >#define themacro(p, i) (p)[(i)][/color]

        by convention, user-defined macros are UPPERCASE so they can be
        identified easier.
        [color=blue]
        >int *ptr, *anotherptr, counter;
        >
        >themacro(ptr , counter)=anothe rptr=MEMORY_ALL OCATION(...);[/color]

        themacro() will create an object (*ptr)[(counter)] which has type int.
        This isn't compatible with anotherptr which is of type int*.
        [color=blue]
        >Where memory_allocati on is a function which mallocs and reallocs
        >memory. I get a weird null pointer somewhere, and just writing:
        >
        >anotherptr=MEM ORY_ALLOCATION( ...);
        >themacro(ptr , counter)=anothe rptr;[/color]
        [color=blue]
        >solves the problem.[/color]

        this is just as bad - if you don't get a warning here, you should turn
        up warninglevels in your compiler.
        [color=blue]
        >Am I doing something out of standard?[/color]

        Remember how macro substitution works - its literal text replacement.
        So you can write down what themacro() will do.


        ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

        Comment

        Working...