Re: Why C has Pointers.

Discussion in 'C Programming' started by Keith Thompson, Dec 7, 2011.

  1. Vincenzo Mercuri <> writes:
    > Il 07/12/2011 13:22, curixinfotech ha scritto:
    >> my Q is on the c language tell me now why c has pointers.

    >
    > here is a short article that can give you a clue as to why and how:
    > http://duramecho.com/ComputerInformation/WhyCPointers.html


    Do you know of another article that doesn't falsely claim that arrays
    are pointers?

    In C, an array variable is just a pointer to a chunk of memory
    where the elements are stored in order

    Arrays *are not* pointers. See section 6 of the comp.lang.c FAQ
    <http://c-faq.com>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 7, 2011
    #1
    1. Advertising

  2. Keith Thompson

    Brian Guest

    On Wed, 07 Dec 2011 11:01:04 -0800, Keith Thompson wrote:
    > Vincenzo Mercuri <> writes:
    >> Il 07/12/2011 13:22, curixinfotech ha scritto:
    >>> my Q is on the c language tell me now why c has pointers.

    >>
    >> here is a short article that can give you a clue as to why and how:
    >> http://duramecho.com/ComputerInformation/WhyCPointers.html

    >
    > Do you know of another article that doesn't falsely claim that arrays
    > are pointers?
    >
    > In C, an array variable is just a pointer to a chunk of memory where
    > the elements are stored in order
    >
    > Arrays *are not* pointers. See section 6 of the comp.lang.c FAQ
    > <http://c-faq.com>.


    You are wrong about that, an array access like a[5] actually translates
    into *(a+5) - pointer operation.

    Cheers Brian
    Brian, Dec 7, 2011
    #2
    1. Advertising

  3. Keith Thompson

    James Kuyper Guest

    On 12/07/2011 03:00 PM, Brian wrote:
    > On Wed, 07 Dec 2011 11:01:04 -0800, Keith Thompson wrote:

    ....
    >> Arrays *are not* pointers. See section 6 of the comp.lang.c FAQ
    >> <http://c-faq.com>.

    >
    > You are wrong about that, an array access like a[5] actually translates
    > into *(a+5) - pointer operation.


    Did you bother following that link? Before asserting that arrays are
    pointers, try comparing sizeof(a) with sizeof(&a[0]); if they happen to
    be equal, double the length of the array, and try it again. Also, please
    explain why the following does NOT work:

    int a[10];
    int **p = &a;
    James Kuyper, Dec 7, 2011
    #3
  4. On Thursday, December 8, 2011 4:30:58 AM UTC+8, James Kuyper wrote:
    > On 12/07/2011 03:00 PM, Brian wrote:
    > > On Wed, 07 Dec 2011 11:01:04 -0800, Keith Thompson wrote:

    > ...
    > >> Arrays *are not* pointers. See section 6 of the comp.lang.c FAQ
    > >> <http://c-faq.com>.

    > >
    > > You are wrong about that, an array access like a[5] actually translates
    > > into *(a+5) - pointer operation.

    >
    > Did you bother following that link? Before asserting that arrays are
    > pointers, try comparing sizeof(a) with sizeof(&a[0]); if they happen to
    > be equal, double the length of the array, and try it again. Also, please
    > explain why the following does NOT work:
    >
    > int a[10];
    > int **p = &a;


    How a multi-dimensional array in C is implemented? The pointer stores an
    address. What a void pointer variable stores( just an address) and points to a value type which can be different in C.
    88888 Dihedral, Dec 7, 2011
    #4
  5. 88888 Dihedral <> writes:
    > On Thursday, December 8, 2011 4:30:58 AM UTC+8, James Kuyper wrote:
    >> On 12/07/2011 03:00 PM, Brian wrote:
    >> > On Wed, 07 Dec 2011 11:01:04 -0800, Keith Thompson wrote:

    >> ...
    >> >> Arrays *are not* pointers. See section 6 of the comp.lang.c FAQ
    >> >> <http://c-faq.com>.
    >> >
    >> > You are wrong about that, an array access like a[5] actually translates
    >> > into *(a+5) - pointer operation.

    >>
    >> Did you bother following that link? Before asserting that arrays are
    >> pointers, try comparing sizeof(a) with sizeof(&a[0]); if they happen to
    >> be equal, double the length of the array, and try it again. Also, please
    >> explain why the following does NOT work:
    >>
    >> int a[10];
    >> int **p = &a;

    >
    > How a multi-dimensional array in C is implemented?

    [...]

    A multi-dimensional array is simply an array of arrays.

    There are data structure that behave like multidimensional arrays using
    arrays of pointers. For example, you can have an array of double*
    elements, where each element points to the first element of an array of
    double. The syntax for accessing an element (a[x][y]) is similar, but
    it's not a multidimensional array in the sense that the C standard uses
    the term. Implementations using pointers are much more flexible, but
    require more housekeeping work to allocate and deallocate memory.

    Section 6 of the comp.lang.c FAQ, <http://c-faq.com>, is an excellent
    resource.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 7, 2011
    #5
  6. Keith Thompson

    James Kuyper Guest

    On 12/08/2011 01:39 PM, Vincenzo Mercuri wrote:
    > Il 07/12/2011 21:30, James Kuyper ha scritto:
    > ...
    >> ... Before asserting that arrays are
    >> pointers, try comparing sizeof(a) with sizeof(&a[0]); if they happen to
    >> be equal, double the length of the array, and try it again. ...

    >
    > Okay:
    >
    > #include <stdio.h>
    >
    > int f(int a[])
    > {
    > return sizeof(a) == sizeof(&a[0]);
    > }

    ....
    > /* just kidding... :) */


    For Brian's benefit, I'll point out that while, in this context, 'a' IS
    a pointer, it's also NOT an array, appearances to the contrary
    notwithstanding, so it doesn't provide support for his claims. I suspect
    that those "appearances to the contrary" are part of the motivation for
    such claims.
    James Kuyper, Dec 8, 2011
    #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

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    862
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,761
    Smokey Grindel
    Dec 2, 2006
  3. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    656
  4. Nick Keighley

    Re: Why C has Pointers.

    Nick Keighley, Dec 8, 2011, in forum: C Programming
    Replies:
    5
    Views:
    309
    Nick Keighley
    Dec 10, 2011
  5. ArifulHossain tuhin

    Why pointers to pointers used

    ArifulHossain tuhin, Feb 28, 2012, in forum: C Programming
    Replies:
    16
    Views:
    449
    Stephen Sprunk
    Mar 1, 2012
Loading...

Share This Page