Quote:
>>In the first program it was OK to increment the address of array A[10]
>>by simply doing A++. But in the second program I kept getting a message
> Eeek ! Nonsense ! You are attempting to modify a constant !
eek! read the faq! he is doing no such thing! he is modifying a pointer
(yes, arrays get decayed to pointers), which is therefore passed *by value*.
he can do anything he wants to it.
Quote:
>>void sum_array(int A[], int *ptr, int n)
>>{
>>int i;
>>for(i=0; i < n; i++)
>> {
>> *ptr += *A;
>> printf("Total is %d\n", *ptr);
>> A++;
>> }
>>}
> This function is broken.
no it isn't. there is absolutely nothing wrong with this function. i would
prefer to declare A is an 'int *' instead of an 'int []', but it's a
question of style. of course there is the chance that the OP was confused
and thought that since A was declared as 'int []', it was an array, but i
don't see any evidence of that.
Quote:
> - A is constant and points to read-only. Better to use a pointer pA
A is not constant. where did you get that idea from? by definition, no
values passed to a function are constant; it's up to the function to add
restrictions like that onto any arguments. of course what the arguments
points to is very often constant, but that's another matter entirely.
Quote:
> - ptr is constant and points to read-write. It is a pointer to the output,
> so I change it's name to pOut;
> void sum_array(int const *const pA, int *const pOut, int n)
what on earth is this garbage? 'const int *pOut' would be acceptable, but
not at necessary. 'int *const pA' would also be acceptable. but pA itself
is NOT constant; it's just a simple pointer. maybe if you could be bothered
to read the faq?? what you've done is created an unnecessary restriction on
pA. the only thing you accomplish by adding the 'const' on pA is making
life harder for yourself.
Quote:
> {
> if(pA)
i believe the OP designed the function in a way that 'A' (your 'pA') and
'ptr' (your 'pOut') exist. there's no need to check to see if they're NULL
or not. i suppose you can never be too safe though :)
Quote:
> {
> int i;
> for(i=0; i < n; i++)
> {
> if(pOut)
again. and if you really wanted to check to see if pOut was NULL, check at
the beginning of the function, not for each iteration :)
Quote:
> {
> *pOut += *pA;
> printf("Total is %d\n", *pOut); /* debug only I presume...
> */
> }
> pA++;
> }
> }
> }
> This will produce a compiler error : "attempting to modify a const object"
> here :
> pA++;
duh! you explicitly declared pA as const, what did you expect?
Quote:
> This will show a design problem. You have received the address of an array,
> and you modify it in the function. This is really a bad idea. Change it to :
no, this is not a bad idea. what you received is a *pointer*, which was
passed *by value*, which points to the array. the OP's function was
actually correct, and was far more readable than yours.
Quote:
> The previous was a nice reusable function, but I you really want to break
> this function back to main, do it from it. I won't do it myself (I don't
> break my toys sorry)
>>#include<stdio.h>
>>main()
>>{
>>int i, total = 0;
>>int *ptr;
>>int A[10] = {1,2,3,4,5,6,7,8,9,10};
>>ptr = &total;
>>for(i=0; i < 10; i++)
>> {
>> *ptr += *A;
>> printf("Total is %d\t", *ptr);
>> A++;
> A is constant, and may not be modified.
indeed. this was covered by another post in this thread i believe, but what
would clc be without 5 people answering the same question in the same way?
:)
anyway, take a look at the faq at questions 4.11 and 6.4 in particular.
there is nothing in the c language that says that an array passed as a
pointer is constant, mainly because it wouldn't make any sense.
the OP should, of course, look at questions 6.4, 6.7, 6.8, or maybe just
section 6 in general :)
cheers
--
/"\ m i k e b u r r e l l
X AGAINST HTML MAIL http://mikpos.dyndns.org
/ \