Array managed by preprocessor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • /* frank */

    #1

    Array managed by preprocessor

    My teacher said that array in C is managed by preprocessor.

    Preprocesser replace all array occurences (i.e. int a[10] ) with
    something that I don't understand/remember well.

    What's exactly happens with array during preprocessing/compiling stage?

    Thanks in advance
  • Artie Gold

    #2
    Re: Array managed by preprocessor

    /* frank */ wrote:[color=blue]
    > My teacher said that array in C is managed by preprocessor.
    >
    > Preprocesser replace all array occurences (i.e. int a[10] ) with
    > something that I don't understand/remember well.
    >
    > What's exactly happens with array during preprocessing/compiling stage?
    >
    > Thanks in advance[/color]

    Well, there are two possibilities here:

    1) You misunderstood what your teacher said.
    2) Your teacher is horribly, horribly wrong.

    I suspect you need to revisit the subject; why not *ask your teacher*?

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas

    "What they accuse you of -- is what they have planned."

    Comment

    • /* frank */

      #3
      Re: Array managed by preprocessor

      Artie Gold ha scritto:
      [color=blue]
      > 1) You misunderstood what your teacher said.[/color]

      If I have this array:

      a[i]

      the preprocessor translate it, into *(a+i)

      a is translated as costant.


      Thanks

      Comment

      • Joona I Palaste

        #4
        Re: Array managed by preprocessor

        /* frank */ <__frank__@desp ammed.com> scribbled the following:[color=blue]
        > Artie Gold ha scritto:[color=green]
        >> 1) You misunderstood what your teacher said.[/color][/color]
        [color=blue]
        > If I have this array:[/color]
        [color=blue]
        > a[i][/color]
        [color=blue]
        > the preprocessor translate it, into *(a+i)[/color]
        [color=blue]
        > a is translated as costant.[/color]

        The preprocessor doesn't translate it into anything. Its job is done at
        that stage and it has exited stage left. [] is a full-blown operator in
        C. Its behaviour is defined as the * (indirection) and + (addition)
        operators combined so that a[i] means *(a+i). Also, i[a] means the same
        thing as a[i].

        --
        /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
        \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
        "A bicycle cannot stand up by itself because it's two-tyred."
        - Sky Text

        Comment

        • Emmanuel Delahaye

          #5
          Re: Array managed by preprocessor

          In 'comp.lang.c', /* frank */ <__frank__@desp ammed.com> wrote:
          [color=blue]
          > My teacher said that array in C is managed by preprocessor.[/color]

          Get a better teacher, and read your C-book about arrays. Do exercices.

          --
          -ed- get my email here: http://marreduspam.com/ad672570
          The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
          C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
          FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

          Comment

          • Malcolm

            #6
            Re: Array managed by preprocessor


            "/* frank */" <__frank__@desp ammed.com> wrote[color=blue]
            >
            > If I have this array:
            >
            > a[i]
            >
            > the preprocessor translate it, into *(a+i)
            >
            > a is translated as costant.
            >[/color]
            You can often get a preprocessor as a standalone program (cpp or something
            similar). Why not run it on some C source and see what happens?


            Comment

            • Case -

              #7
              Re: Array managed by preprocessor

              /* frank */ wrote:[color=blue]
              > My teacher said that array in C is managed by preprocessor.
              >
              > Preprocesser replace all array occurences (i.e. int a[10] ) with
              > something that I don't understand/remember well.[/color]

              Was it perhaps that a[10] is equivalent with 10[a], because
              a compiler may transform a[10] into *(a + 10) and 10[a] in
              *(10 + a)?
              [color=blue]
              >
              > What's exactly happens with array during preprocessing/compiling stage?[/color]

              The preprocessor is a kind of simple text processor with commands
              that start with (and use) #. The preprocessing 'stage' is a
              purely textual one; the meaning of C constructs like a[10] is
              not know there. So only when 'a' would have been #defined, the
              preprocessor will modify it textually (i.e., replace it with
              what 'a' was #defined to be).

              The compiler deals with the meaning of the (preprocessed C) code.
              In differerent stages (using different internal representations ),
              the compiler transforms the C code into machine code. Often the
              compiler generates assembly code, which is transformed into the
              actual machine code by the assembler program.

              Comment

              • Paul Mensonides

                #8
                Re: Array managed by preprocessor

                "Case -" <no@no.no> wrote in message
                news:40def117$0 $62354$5fc3050@ dreader2.news.t iscali.nl...
                [color=blue][color=green]
                > > What's exactly happens with array during preprocessing/compiling stage?[/color]
                >
                > The preprocessor is a kind of simple text processor with commands
                > that start with (and use) #. The preprocessing 'stage' is a
                > purely textual one; the meaning of C constructs like a[10] is
                > not know there. So only when 'a' would have been #defined, the
                > preprocessor will modify it textually (i.e., replace it with
                > what 'a' was #defined to be).[/color]

                The preprocessor operates on preprocessing tokens. It is not a text processor
                except during initial tokenization. Otherwise, you're right. The preprocessor
                does not transform "a[10]". In fact, even if 'a' was defined as a macro, there
                is no way that macro expansion can extract the '10'.

                Regards,
                Paul Mensonides


                Comment

                • Peter Ammon

                  #9
                  Re: Array managed by preprocessor

                  /* frank */ wrote:
                  [color=blue]
                  > My teacher said that array in C is managed by preprocessor.
                  >
                  > Preprocesser replace all array occurences (i.e. int a[10] ) with
                  > something that I don't understand/remember well.
                  >
                  > What's exactly happens with array during preprocessing/compiling stage?
                  >
                  > Thanks in advance[/color]

                  Let's find out.

                  peter ) cat test.c
                  void function(void) {
                  int array[10];
                  array[1]=2;
                  array[5]=array[1];
                  }
                  peter ) cc -E test.c
                  # 1 "test.c"
                  #pragma GCC set_debug_pwd "/Users/peter"
                  # 1 "<built-in>"
                  # 1 "<command line>"
                  # 1 "test.c"
                  void function(void) {
                  int array[10];
                  array[1]=2;
                  array[5]=array[1];
                  }

                  Looks like nothing at all!

                  --
                  Pull out a splinter to reply.

                  Comment

                  • /* frank */

                    #10
                    Re: Array managed by preprocessor

                    Emmanuel Delahaye ha scritto:
                    [color=blue]
                    > Get a better teacher, and read your C-book about arrays. Do exercices.[/color]
                    K&R 2nd ed. writes that C compiler transform the expression
                    a[i] in *(a+i) so some kind of "conversion " happen, during
                    preprocessing stage?
                    Tks

                    Comment

                    • Martin Ambuhl

                      #11
                      Re: Array managed by preprocessor

                      /* frank */ wrote:[color=blue]
                      > Emmanuel Delahaye ha scritto:
                      >[color=green]
                      >> Get a better teacher, and read your C-book about arrays. Do exercices.[/color]
                      >
                      > K&R 2nd ed. writes that C compiler transform the expression
                      > a[i] in *(a+i) so some kind of "conversion " happen, during
                      > preprocessing stage?[/color]

                      The compiler does many things other than in the preprocessing stage.
                      The compiler also emits some sort of code. Do you think the
                      preprocessor does that, too?

                      Comment

                      • Case

                        #12
                        Re: Array managed by preprocessor

                        Paul Mensonides wrote:[color=blue]
                        > "Case -" <no@no.no> wrote in message
                        > news:40def117$0 $62354$5fc3050@ dreader2.news.t iscali.nl...[color=green][color=darkred]
                        >>>What's exactly happens with array during preprocessing/compiling stage?[/color]
                        >>
                        >>The preprocessor is a kind of simple text processor with commands
                        >>that start with (and use) #. The preprocessing 'stage' is a
                        >>purely textual one; the meaning of C constructs like a[10] is
                        >>not know there. So only when 'a' would have been #defined, the
                        >>preprocesso r will modify it textually (i.e., replace it with
                        >>what 'a' was #defined to be).[/color]
                        >
                        > The preprocessor operates on preprocessing tokens. It is not a text processor
                        > except during initial tokenization.[/color]

                        Yes it is, CPP processes a text file (which happens to be C code),
                        it's as simple as that. You use 'initial tokenization' without
                        supplying the context (do you mean as part of C compilation, are
                        you talking about the internals of CPP, or ...). What do you mean?

                        Case

                        Comment

                        • Dan Pop

                          #13
                          Re: Array managed by preprocessor

                          In <40dfd783$0$424 17$e4fe514c@new s.xs4all.nl> Case <no@no.no> writes:
                          [color=blue]
                          >Paul Mensonides wrote:[color=green]
                          >> "Case -" <no@no.no> wrote in message
                          >> news:40def117$0 $62354$5fc3050@ dreader2.news.t iscali.nl...[color=darkred]
                          >>>>What's exactly happens with array during preprocessing/compiling stage?
                          >>>
                          >>>The preprocessor is a kind of simple text processor with commands
                          >>>that start with (and use) #. The preprocessing 'stage' is a
                          >>>purely textual one; the meaning of C constructs like a[10] is
                          >>>not know there. So only when 'a' would have been #defined, the
                          >>>preprocess or will modify it textually (i.e., replace it with
                          >>>what 'a' was #defined to be).[/color]
                          >>
                          >> The preprocessor operates on preprocessing tokens. It is not a text processor[/color][/color]
                          ^^^^^^^^^^^^^^^ ^^^^^[color=blue][color=green]
                          >> except during initial tokenization.[/color]
                          >
                          >Yes it is, CPP processes a text file (which happens to be C code),
                          >it's as simple as that.[/color]

                          Nope, it isn't. Try avoiding topic you don't have any clue about.
                          [color=blue]
                          >You use 'initial tokenization' without
                          >supplying the context (do you mean as part of C compilation, are
                          >you talking about the internals of CPP, or ...). What do you mean?[/color]

                          He means what he says: preprocessing tokens, as described by the C
                          language definition, which is the implicit context in this newsgroup.

                          If you still don't get it, consider the following example:

                          #if 0
                          That's all
                          #endif

                          and compare it with

                          /* That's all */

                          The first commenting method is valid only for correct C code, or, at least
                          sequences of valid C preprocessing tokens.

                          Dan
                          --
                          Dan Pop
                          DESY Zeuthen, RZ group
                          Email: Dan.Pop@ifh.de

                          Comment

                          • Dan Pop

                            #14
                            Re: Array managed by preprocessor

                            In <2k83qpF191ss5U 1@uni-berlin.de> /* frank */ <__frank__@desp ammed.com> writes:
                            [color=blue]
                            >My teacher said that array in C is managed by preprocessor.[/color]

                            Find another teacher. If this is not an option, learn C from K&R2: your
                            teacher doesn't know what he's talking about.

                            Dan
                            --
                            Dan Pop
                            DESY Zeuthen, RZ group
                            Email: Dan.Pop@ifh.de

                            Comment

                            • Chris Dollin

                              #15
                              Re: Array managed by preprocessor

                              /* frank */ wrote:
                              [color=blue]
                              > Artie Gold ha scritto:
                              >[color=green]
                              >> 1) You misunderstood what your teacher said.[/color]
                              >
                              > If I have this array:
                              >
                              > a[i][/color]

                              You mean, if you have this subscript expression. a[i] isn't
                              an array unless a is an array of arrays.
                              [color=blue]
                              > the preprocessor translate it, into *(a+i)[/color]

                              No, it doesn't. "Preprocess ing" is a very specific stage in C
                              translation, and it doesn't do anything with expressions a[i]
                              (unless they're in a preprocessing expression).

                              a[i] *means* *(a+i), but that's nothing to do with the C
                              preprocessor.
                              [color=blue]
                              > a is translated as costant.[/color]

                              That statement is too vague to be wrong.

                              --
                              Chris "electric hedgehog" Dollin
                              C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
                              C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html

                              Comment

                              Working...