Reg : Pointers

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

    #1

    Reg : Pointers

    Can any one give me indetail about,

    what are pointers?
    How are pointers useful in C prgramming?
    How do they work?

    i had gone through one book, i am a bit confused.

    Regards,
    Satish...
  • Anthony Borla

    #2
    Re: Pointers

    "Satish Kumar" <skravipati@gma il.com> wrote in message
    news:69a96c91.0 504010111.3f539 b33@posting.goo gle.com...[color=blue]
    > Can any one give me indetail about,
    >
    > what are pointers?
    > How are pointers useful in C prgramming?
    > How do they work?
    >
    > I had gone through one book, i am a bit confused.
    >[/color]

    I *do* have a pointer for you:

    Google

    using the phrase:

    Pointers C Tutorial

    Cheers,

    Anthony Borla


    Comment

    • Jens.Toerring@physik.fu-berlin.de

      #3
      Re: Reg : Pointers

      Satish Kumar <skravipati@gma il.com> wrote:[color=blue]
      > Can any one give me indetail about,[/color]
      [color=blue]
      > what are pointers?[/color]

      Pointers are variables that can hold addresses. Normally, they get
      assigned a value that is the address of another object (a normal
      variable, a function or another pointer). Once that has happened
      you can access what the pointer points to by dereferencing it,
      which is done by putting the (unary) '*' operator in front of
      the pointer variables name.

      So when you have e.g.

      int i = 5;
      int *ip = &i; /* pointer, pointing to i */

      then you can e.g. do

      printf( "i is %d\n", *ip );

      or

      *ip = 7; /* change i indirectly via the pointer */

      printf( "i is now %d\n", i )

      and this will print out

      i is now 7
      [color=blue]
      > How are pointers useful in C prgramming?[/color]

      First of all, they allow you to pass addresses of variables to
      functions, and by doing so it is possible to change the content
      of the variable pointed to from within the function (remember:
      in C variables are passed by value, so a function only receives
      the value of an argument variable and can't change the variable
      itself).

      Second, pointers are indispensable when you need amounts of
      memory you can only determine while the program is already
      running. Functions like malloc() return you a pointer to a
      memory buffer with a size you can tell the function. If the
      function succeeds you can use the returned value to access
      that memory, e.g.

      int *a = malloc( 100 * sizeof *int );
      if ( a != NULL ) {
      a[ 17 ] = 42;
      free( a );
      }

      But there are hundreds of other uses of pointers and listing
      only the small subset which immediately comes to mind would
      make this posting much too long. C without pointers would be
      a rather useless language.
      [color=blue]
      > How do they work?[/color]

      What do you mean by "work"?
      [color=blue]
      > i had gone through one book, i am a bit confused.[/color]

      We all started that way (more or less). Keep reading and experi-
      menting and you will understand soon enough;-) Trying to draw
      some boxes representing pointers and variables and arrows de-
      picting the relationship between them sometimes helps (especially
      if you start playing around with pointers to pointers).

      Regards, Jens
      --
      \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
      \______________ ____________ http://www.toerring.de

      Comment

      • Ravi Uday

        #4
        Re: Reg : Pointers



        Satish Kumar wrote:[color=blue]
        > Can any one give me indetail about,
        >
        > what are pointers?
        > How are pointers useful in C prgramming?
        > How do they work?
        >
        > i had gone through one book, i am a bit confused.
        >
        > Regards,
        > Satish...[/color]

        C-FAQ @ Section 4.



        gives a good understanding on Pointers
        All the best :)

        - Ravi

        Comment

        • Ramasubramanian XR (AS/EAB)

          #5
          Re: Reg : Pointers

          On 2005-04-01 11:11, Satish Kumar wrote:[color=blue]
          > Can any one give me indetail about,
          >
          > what are pointers?
          > How are pointers useful in C prgramming?
          > How do they work?
          >
          > i had gone through one book, i am a bit confused.
          >
          > Regards,
          > Satish...[/color]
          you can get good material from net
          search google with keyword as ' pointers in c '
          you can get so many pdf

          Comment

          • A. Sinan Unur

            #6
            Re: Reg : Pointers

            "Ramasubramania n XR (AS/EAB)" <ramasubbu.xr.r amasubramanian@ ericsson.com>
            wrote in news:d2r5t7$16u $2@newstree.wis e.edt.ericsson. se:
            [color=blue]
            > On 2005-04-01 11:11, Satish Kumar wrote:[color=green]
            >> Can any one give me indetail about,
            >>
            >> what are pointers?
            >> How are pointers useful in C prgramming?
            >> How do they work?[/color][/color]

            ....
            [color=blue]
            > you can get good material from net
            > search google with keyword as ' pointers in c '
            > you can get so many pdf[/color]

            Still, one has to choose among what is available.



            Sinan
            --
            A. Sinan Unur <1usa@llenroc.u de.invalid>
            (reverse each component and remove .invalid for email address)

            Comment

            • Jonathan Bartlett

              #7
              Re: Reg : Pointers

              Satish Kumar wrote:[color=blue]
              > Can any one give me indetail about,
              >
              > what are pointers?
              > How are pointers useful in C prgramming?
              > How do they work?[/color]

              I have found that for people to really understand pointers, they need to
              understand assembly language and how it works. I wrote a book to teach
              assembly language programming, not to make people expert assembly
              language programmers, but to instead give them a better idea of how
              their computer works and how the languages they use work under the hood.
              You can find the book at:



              Jon
              ----
              Learn to program using Linux assembly language

              Comment

              • Joona I Palaste

                #8
                Re: Reg : Pointers

                Jonathan Bartlett <johnnyb@eskimo .com> scribbled the following:[color=blue]
                > Satish Kumar wrote:[color=green]
                >> Can any one give me indetail about,
                >>
                >> what are pointers?
                >> How are pointers useful in C prgramming?
                >> How do they work?[/color][/color]
                [color=blue]
                > I have found that for people to really understand pointers, they need to
                > understand assembly language and how it works. I wrote a book to teach
                > assembly language programming, not to make people expert assembly
                > language programmers, but to instead give them a better idea of how
                > their computer works and how the languages they use work under the hood.
                > You can find the book at:[/color]
                [color=blue]
                > http://www.cafeshops.com/bartlettpublish.8640017[/color]

                I strongly disagree. I have only very limited experience about assembly
                language, but I understand the basics about C pointers. In my opinion,
                it's enough to know that variables are stored in memory locations, and
                pointers store the addresses of those locations.

                --
                /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                \-------------------------------------------------------- rules! --------/
                "It sure is cool having money and chicks."
                - Beavis and Butt-head

                Comment

                • Keith Thompson

                  #9
                  Re: Reg : Pointers

                  Mark L Pappin <mlp@acm.org> writes:[color=blue]
                  > Joona I Palaste <palaste@cc.hel sinki.fi> writes:[/color]
                  [...][color=blue][color=green]
                  >> I strongly disagree. I have only very limited experience about
                  >> assembly language, but I understand the basics about C pointers. In
                  >> my opinion, it's enough to know that variables are stored in memory
                  >> locations, and pointers store the addresses of those locations.[/color]
                  >
                  > I'll have to disagree with both of you here. C pointers _may_ (and in
                  > many common implementations do) store just addresses, but at an
                  > abstract level (where one should be thinking, to avoid UB) each
                  > pointer has both value (which may be invalid in certain circumstances)
                  > and type. Thinking of pointers as addresses leads to such brokenness
                  > as expecting[/color]
                  [snip]

                  A good point, but I'm going to quibble anyway.

                  The standard uses the term "address" to mean a pointer value. So if
                  you follow the terminology of the standard, it's trivially true that
                  pointers store just addresses. You just need to be aware that they
                  aren't necessarily "addresses" in the low-level sense used by most
                  people -- just as C "bytes" aren't necessarily 8 bits.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • BGreene

                    #10
                    Re: Reg : Pointers

                    [color=blue]
                    > The standard uses the term "address" to mean a pointer value. So if
                    > you follow the terminology of the standard, it's trivially true that
                    > pointers store just addresses. You just need to be aware that they
                    > aren't necessarily "addresses" in the low-level sense used by most
                    > people.
                    >
                    > --
                    > Keith Thompson (The_Other_Keit h) kst-u@mib.org[/color]
                    <http://www.ghoti.net/~kst>[color=blue]
                    > San Diego Supercomputer Center <*>[/color]
                    <http://users.sdsc.edu/~kst>[color=blue]
                    > We must do something. This is something. Therefore, we must do this.[/color]

                    Could you please clarify this?

                    Barry
                    barryg@highstre am.net


                    Comment

                    • Keith Thompson

                      #11
                      Re: Reg : Pointers

                      "BGreene" <barryg@highstr eam.net> writes:[color=blue]
                      > Keith Thompson <kst-u@mib.org> writes:[color=green]
                      >> The standard uses the term "address" to mean a pointer value. So if
                      >> you follow the terminology of the standard, it's trivially true that
                      >> pointers store just addresses. You just need to be aware that they
                      >> aren't necessarily "addresses" in the low-level sense used by most
                      >> people.[/color]
                      >
                      > Could you please clarify this?[/color]

                      The usual (non-C) sense of the term "address" is a virtual or physical
                      address recognized by the CPU and/or memory management system.

                      The C standard uses the term "address" to mean a C pointer value. For
                      example, the unary "&" (address-of) operator yields an address value.
                      The C standard defines the semantics, but doesn't say much about the
                      representation. An "address" in the C sense could be some
                      higher-level construct than a virtual or physical machine address.
                      It's an address in the C abstract machine, not necessarily in the
                      physical machine.

                      In most implementations , C addresses (pointer values) are machine
                      addresses, but the C standard is designed to allow a variety of
                      implementation strategies.

                      One concrete example is the C implementation on Cray vector machines.
                      A machine-level address is a 64-bit quantity that points to a 64-bit
                      machine word, but the C compiler has CHAR_BIT==8, so it needs a
                      mechanism to point to 8-bit bytes within words. An int* pointer is a
                      machine address, but a char* or void* pointer has a 3-bit offset
                      stored in the otherwise unused high-order bits of a word pointer.
                      This is implemented entirely in code generated by the compiler, not in
                      hardware. A char* pointer value with a non-zero offset field is a C
                      address, but it's not a machine address.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • BGreene

                        #12
                        Re: Reg : Pointers


                        "Keith Thompson" <kst-u@mib.org> wrote in message
                        news:lnd5syxqsd .fsf@nuthaus.mi b.org...[color=blue]
                        > "BGreene" <barryg@highstr eam.net> writes:[color=green]
                        > > Keith Thompson <kst-u@mib.org> writes:[color=darkred]
                        > >> The standard uses the term "address" to mean a pointer value. So if
                        > >> you follow the terminology of the standard, it's trivially true that
                        > >> pointers store just addresses. You just need to be aware that they
                        > >> aren't necessarily "addresses" in the low-level sense used by most
                        > >> people.[/color]
                        > >
                        > > Could you please clarify this?[/color]
                        >
                        > The usual (non-C) sense of the term "address" is a virtual or physical
                        > address recognized by the CPU and/or memory management system.
                        >
                        > The C standard uses the term "address" to mean a C pointer value. For
                        > example, the unary "&" (address-of) operator yields an address value.
                        > The C standard defines the semantics, but doesn't say much about the
                        > representation. An "address" in the C sense could be some
                        > higher-level construct than a virtual or physical machine address.
                        > It's an address in the C abstract machine, not necessarily in the
                        > physical machine.
                        >
                        > In most implementations , C addresses (pointer values) are machine
                        > addresses, but the C standard is designed to allow a variety of
                        > implementation strategies.
                        >
                        > One concrete example is the C implementation on Cray vector machines.
                        > A machine-level address is a 64-bit quantity that points to a 64-bit
                        > machine word, but the C compiler has CHAR_BIT==8, so it needs a
                        > mechanism to point to 8-bit bytes within words. An int* pointer is a
                        > machine address, but a char* or void* pointer has a 3-bit offset
                        > stored in the otherwise unused high-order bits of a word pointer.
                        > This is implemented entirely in code generated by the compiler, not in
                        > hardware. A char* pointer value with a non-zero offset field is a C
                        > address, but it's not a machine address.
                        >
                        > --
                        > Keith Thompson (The_Other_Keit h) kst-u@mib.org[/color]
                        <http://www.ghoti.net/~kst>[color=blue]
                        > San Diego Supercomputer Center <*>[/color]
                        <http://users.sdsc.edu/~kst>[color=blue]
                        > We must do something. This is something. Therefore, we must do this.[/color]


                        Thanks for making this clear. The example you gave was well described. It
                        is similar to the implemetation of all the compilers that ran on HP-PA,
                        with the exception HP-PA was originally 32 bit, and used the 2 bit (low
                        order bits) as a "space register." to allow it to address 4G. I don't know
                        where I read this but I am pretty sure it is true.


                        Comment

                        Working...