Re: changing the value in an Array with C

From: Tieka (cmhustonNOSPAM_at_mts.net)
Date: 02/15/04


Date: Sun, 15 Feb 2004 05:37:46 -0500

In article <c0kai8$ppf$1@sparta.btinternet.com>, binary@eton.powernet.co.uk
wrote:

> Tieka wrote:
>
> > I am trying to learn C after many years out of school, and some there are
> > some things I just can't get my head around, notably pointers, references
> > and passing arrays. I have tried various tutorials and online references,
> > but I can't find anything that applies to what I want to do. I hope
> > someone here can clear things up for me.
>
> Mike Wahler's answer is excellent, and I recommend it to you. Rather than
> address your specific question, then (which he has already done), I'd like
> to answer your more general question about pointers, references, and
> passing arrays.

Thank you for the general answers. I will keep a copy of your post in my
notes file for future reference.

>
> At its simplest, a pointer can be thought of as an object (not an
> object-oriented object; just an ordinary, common C object such as you might
> find in any kitchen garden or vegetable patch), which holds as its value
> the address of another object (which may indeed be the first - or second or
> third or Nth - object in an array of objects).
>
> We don't have to worry about references because C has none (in the C++
> sense). C's lovely like that - nice and simple.
>
> As Mike rightly pointed out, you can't pass an array by value in C (well,
> you can, but only if you cheat - and you almost certainly aren't going to
> do it by accident, at least not for some time yet).
>
> What you /can/ do is pass a pointer...
>
> > What I want to do is fill an array in main(), then send *something* to a
> > function. This function then changes values in the array. When I get back
> > to main, I can access the new values in the array.
>
> ...and *something*, in this case, is a pointer to the first element in the
> array. Since arrays are laid out contiguously in memory, this means that -
> provided it knows when to stop - any function with a pointer to your
> array's /first/ element can /also/ work out the addresses of all the other
> elements. So, if your array is:
>
> double PeterPan[100];
>
> then, to "pass" the array to a function, foo, we need simply do this:
>
> foo(&PeterPan[0], sizeof PeterPan / sizeof PeterPan[0]);
>
> The expression in the second argument merely calculates the number of
> elements in the array - that's because the foo() function will /not/ be
> able to do this for itself.
>
> And now...
>
>
> ***** * * **** *** * * * ****
> * * * * * * * * * *
> * **** *** *** * * * ***
> * * * * * * * * * *
> * * * **** * * ** **** ****
>
>
>
> The final piece of the puzzle is what Chris Torek calls The Rule: in a value
> context, the name of an array is synonymous with the address of its first
> element.
>
> In fact, the real rule is this: that *(a + i) and a[i] are synonymous (in a
> value context). Therefore, &*(a + i) and &a[i] are synonymous.
>
> Since & and * cancel, (a + i) is synonymous with &a[i]
>
> Set i to 0, and we have
>
> (a + 0) is synonymous with &a[0]
>
> And (a + 0) is just another way of writing a.
>
> So (in a value context) the name of an array is synonymous with the address
      ^^^^^^^^^^^^^^^^^^^^
That helps me out a lot. Many of the tutorials and FAQs I have looked at
are not clear on whether they are talking about reference or value. It has
always seemed odd to me that you can refer to an array without the
subscript notation, but now I understand where it comes from.

> of its first element. Thus, instead of writing:
>
> foo(&PeterPan[0], sizeof PeterPan / sizeof PeterPan[0]);
>
> we could just as easily have written
>
> foo(PeterPan, sizeof PeterPan / sizeof PeterPan[0]);
>
>
> > Now I am assuming the right way is to use pointers, instead of passing the
> > entire array.
>
> Because of The Rule, it's actually impossible to pass the array, but (again
> because of The Rule) it turns out not to matter.
>

tieka,
not so confused now.

-- 
()  ascii ribbon campaign - against html e-mail
/\                        - against microsoft attachments


Relevant Pages

  • Re: list implementation
    ... >I believe the type "list" is implemented as an array of pointers. ... A Python list is sematically/behaviorally defined as a mutable extensible ... the references are arrays of C pointers. ...
    (comp.lang.python)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • [RFCv2][PATCH] flexible array implementation
    ... I call it a flexible array. ... storage for pointers to the second level. ... all locking must be provided by the caller. ... make sure to pass in &ptr instead of ptr. ...
    (Linux-Kernel)
  • [RFC][PATCH] flexible array implementation v4
    ... I call it a flexible array. ... so never does an order>0 allocation. ... storage for pointers to the second level. ... all locking must be provided by the caller. ...
    (Linux-Kernel)
  • Re: [RFC][PATCH] flexible array implementation v4
    ... I call it a flexible array. ... so never does an order>0 allocation. ... storage for pointers to the second level. ... all locking must be provided by the caller. ...
    (Linux-Kernel)