Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Pointers, constant values |
| Date | 2015-10-16 19:34 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <mvrfri$j7e$1@dont-email.me> (permalink) |
| References | <5b2ab074-7644-4798-9b90-a3ae47e1f2b8@googlegroups.com> <ln4mhq7s4z.fsf@kst-u.example.com> <mvr9nb$ra4$1@dont-email.me> <KoaUx.29137$os5.11289@fx14.iad> |
On 16/10/2015 18:23, Lew Pitcher wrote:
> On Friday October 16 2015 12:49, in comp.lang.c, "Bartc" <bc@freeuk.com>
> wrote:
>> On 16/10/2015 16:25, Keith Thompson wrote:
>>> Not everyone will agree with this; some will argue that using the []
>>> notation is a reminder that it points to an element of an array, not
>>> just a single int object.
>>
>> But 'int *array' *is* a pointer to a single element. C allows you to
>> pass a pointer to an actual array, if not the array itself:
>>
>> int array_sum(int (*array)[], int n);
>
> That's one way of looking at it. OTOH, you can also interpret <<array>> above
> as a pointer-to a pointer-to an int. In other words, you haven't really
> passed "a pointer to an actual array", but instead have passed a pointer to a
> pointer (that, hopefully, points to at least one element of an array). Now,
> you have two problems instead of one.
No, I've passed a pointer to an array. (Cdecl tells me the function
takes a 'pointer to array of int'.)
Perhaps you're confused by the fact that, in order to do access an
element of the array inside the function, then the array name, 'array',
'decomposes' to a pointer to the first element. In other words, int**.
But this is just a C thing.
As an example, if I have 'static int A[100]'; in the caller, where A is
at address 0x4674, and call array_sum(A,1), then I'm passing 0x4674. I'm
not passing the address of an another location that contains 0x4764.
While inside array_sum(), it's only doing one dereference to access
A[0]. (That extra dereference is another bit of a confusion that C likes
to throw your way.)
>> However this also now requires proper array dereferencing and indexing,
>> not just applying an offset to an arbitrary pointer which can be to
>> anything:
>>
>> (*array)[i]
>
> I fail to see how this suggestion simplifies or solves the problem. It /does/
> add another pointer,
No, it doesn't. But it does mean that you have to explicitly dereference
this pointer-to-array in a way that you don't need with pointer-to-int.
And the syntax for that is ugly. That's why nobody uses it.
> which will have to be validated,
That's the idea: that you don't unintentionally pass something that is
not a pointer to an array. Take these three versions of array_sum:
int array_sum1(int X[], int n) {return X[0];}
int array_sum2(int *X, int n) {return X[0];}
int array_sum3(int (*X)[], int n) {return (*X)[0];}
The third of these passes an actual pointer-to-array. In the caller:
int B[100];
array_sum1(B,100);
array_sum2(B,100);
array_sum3(&B,100);
Notice that the third requires you to explicitly pass the
pointer-to-array, and won't accept just 'B', which is a pointer-to-int.
It's clearer here:
int C;
array_sum1(&C,100);
array_sum2(&C,100);
array_sum3(&C,100);
The compiler doesn't complain about the first two; that's the problem.
It does rightly complain about the third form.
> I would rephrase that as
> some will argue that using the [] notation is a reminder that it IS INTENDED
> to point to an element of an array, not just a single int object.
> (caps to show the difference in phrasing)
> Hopefully, when using the [] notation, both the caller and the called
> function /agree/ that the parameter is intended to point to an array.
This is just another weird thing about C. Because arrays can't be passed
to functions by value, then instead of a reporting an error when 'int
A[]' is seen in a parameter list, the meaning is changed to 'int* A'. In
fact I think all types involving [] in parameter lists are one pointer
level out of step with the same type spec anywhere else.
--
Bartc
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Pointers, constant values Alla _ <modelling.data@gmail.com> - 2015-10-16 00:49 -0700
Re: Pointers, constant values Philip Lantz <prl@canterey.us> - 2015-10-16 01:49 -0700
Re: Pointers, constant values James Kuyper <jameskuyper@verizon.net> - 2015-10-16 09:41 -0400
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-16 08:55 -0700
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 08:25 -0700
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-16 09:11 -0700
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 12:06 -0700
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-16 12:55 -0700
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-16 17:49 +0100
Re: Pointers, constant values Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-10-16 13:23 -0400
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-16 19:34 +0100
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-16 12:05 -0700
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 12:18 -0700
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 12:13 -0700
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-16 20:33 +0100
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 13:01 -0700
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-16 21:09 +0100
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 13:41 -0700
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-16 21:07 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-16 21:20 +0100
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-16 22:02 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-16 22:21 +0100
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-16 14:39 -0700
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 15:26 -0700
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-16 15:44 -0700
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 16:58 -0700
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-16 17:28 -0700
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-16 22:50 +0100
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-16 15:14 -0700
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-17 01:23 +0100
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-17 19:39 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-17 22:42 +0100
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-18 00:31 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-18 02:11 +0100
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-18 11:59 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-18 12:12 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-18 12:28 +0100
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-18 23:16 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-18 23:34 +0100
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-19 01:45 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-19 11:51 +0100
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-19 12:55 +0100
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-19 13:51 +0100
Re: Pointers, constant values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-19 06:02 -0700
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-19 07:02 -0700
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-19 15:39 +0100
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-19 08:54 -0700
Re: Pointers, constant values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-19 09:14 -0700
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-19 10:21 -0700
Re: Pointers, constant values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-19 12:57 -0700
Re: Pointers, constant values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-19 21:20 +0000
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-20 20:50 -0700
Re: Pointers, constant values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-21 04:58 +0000
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-19 08:40 -0700
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-19 20:17 +0100
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-17 16:44 -0700
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-18 11:52 +0100
Re: Pointers, constant values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-18 05:15 -0700
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-18 21:54 +0100
Re: Pointers, constant values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-18 16:09 -0700
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-18 07:55 -0700
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-18 21:47 +0100
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-18 15:37 -0700
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-19 00:10 +0100
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-18 19:04 -0700
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-16 21:13 +0100
Re: Pointers, constant values Ian Collins <ian-news@hotmail.com> - 2015-10-17 11:27 +1300
Re: Pointers, constant values Bartc <bc@freeuk.com> - 2015-10-17 00:56 +0100
Re: Pointers, constant values Les Cargill <lcargill99@comcast.com> - 2015-10-16 12:37 -0500
Re: Pointers, constant values James Kuyper <jameskuyper@verizon.net> - 2015-10-16 14:22 -0400
Re: Pointers, constant values Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-16 18:57 +0000
Re: Pointers, constant values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-16 20:40 +0100
Re: Pointers, constant values Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-16 20:46 +0000
Re: Pointers, constant values Keith Thompson <kst-u@mib.org> - 2015-10-16 14:24 -0700
Re: Pointers, constant values Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-19 12:43 +0000
Re: Pointers, constant values Siri Cruz <chine.bleu@yahoo.com> - 2015-10-16 13:20 -0700
Re: Pointers, constant values supercat@casperkitty.com - 2015-10-17 13:59 -0700
csiph-web