Pointers & incrementing array address 
Author Message
 Pointers & incrementing array address

Hello,

The first simple program below uses one function call to sum up the
values in one integer array (A[10]).  The program uses one pointer and
works beautifully.  However, when I tried to repeat the program without
the use of any function I kept getting an error message.

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
that I needed an lvalue with A++.  Why will A++ work in the first
program and not the second one?

#include<stdio.h>  // Below program compiles

void sum_array(int [], int *, int);

main() {
int total = 0;
int A[10] = {1,2,3,4,5,6,7,8,9,10};
    sum_array(A, &total, 10);

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++;
   }

Quote:
}

The program below recreates the one above to the best of my ability, but
the error message I get is "Lvalue required" where the A++ is.

#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++;
   }

Quote:
}

Thanks!

Chris N.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Thu, 30 May 2002 03:00:00 GMT  
 Pointers & incrementing array address

Quote:

> #include<stdio.h>  // Below program compiles

> void sum_array(int [], int *, int);

> main() {

This implicit form is illegal by the new standard:

int main(void)

Quote:
> int total = 0;
> int A[10] = {1,2,3,4,5,6,7,8,9,10};
>     sum_array(A, &total, 10);

return 0;

Quote:
> }

> void sum_array(int A[], int *ptr, int n)

The declaration "int A[]" in a function's parameter list, even though it
looks like an array, in fact declares a pointer, just as if you had used
"int *A". Consequently, pointer arithmetic is allowed on it.

Quote:
> {
> int i;
> for(i=0; i < n; i++)
>    {
>    *ptr += *A;
>    printf("Total is %d\n", *ptr);
>    A++;
>    }
> }

> The program below recreates the one above to the best of my ability, but
> the error message I get is "Lvalue required" where the A++ is.

> #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++;
>    }
> }

In this second program, A is a real array, so the ++ operator can't be
used on it. Use i to index it or declare it as "int *A".

Gergo

--
Everything should be made as simple as possible, but not simpler.
                -- Albert Einstein

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Thu, 30 May 2002 03:00:00 GMT  
 Pointers & incrementing array address

Quote:
>Hello,

>The first simple program below uses one function call to sum up the
>values in one integer array (A[10]).  The program uses one pointer and
>works beautifully.  However, when I tried to repeat the program without
>the use of any function I kept getting an error message.

>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 !

Quote:
>that I needed an lvalue with A++.  Why will A++ work in the first
>program and not the second one?

Thanks you compiler ! It has avoided your hard drive to be reformatted, or
WW III to be launched, how knows...

Quote:
>#include<stdio.h>  // Below program compiles

>void sum_array(int [], int *, int);

>main() {
>int total = 0;
>int A[10] = {1,2,3,4,5,6,7,8,9,10};
>    sum_array(A, &total, 10);
>}

>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.
- A is constant and points to read-only. Better to use a pointer pA
- ptr is constant and points to read-write. It is a pointer to the output,
so I change it's name to pOut;

It may be rewritten like this :

void sum_array(int const *const pA, int *const pOut, int n)
{
    if(pA)
    {
    int i;
        for(i=0; i < n; i++)
       {
          if(pOut)
          {
               *pOut += *pA;
                 printf("Total is %d\n", *pOut); /* debug only I presume...
*/
          }
       pA++;
       }
    }

Quote:
}

This will produce a compiler error : "attempting to modify a const object"
here :
       pA++;

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 :

void sum_array(int const *const pA, int *const pOut, int n)
{
int const *p=pA;

   if(p)
   {
   int i;

       for(i=0; i < n; i++)
       {
          if(pOut)
          {
             *pOut += *pA;
          }
       pA++;
       }
   }

Quote:
}
>The program below recreates the one above to the best of my ability, but
>the error message I get is "Lvalue required" where the A++ is.

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)

Quote:
>#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.

Quote:
>   }
>}

--
HS
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks. -- Chris Dollin CLC


Thu, 30 May 2002 03:00:00 GMT  
 Pointers & incrementing array address

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
              / \



Thu, 30 May 2002 03:00:00 GMT  
 Pointers & incrementing array address

Thanks to everyone who took the time to answer my post.  I hadn't
realized that when an array gets passed to a function, it actually gets
passed as a pointer so that pointer arithmetic can be done on it.  I had
not been aware of any of this until now.  I suppose the answer was
lodged somewhere in my Standard C textbook (which normally is a very
good book) but if so I never found it.  It was frustrating to read in
the book how pointer arithmetic COULD NOT be done on arrays, and then to
have the programming example given to me by my professor, which showed
just the opposite.

Thanks again!  The few times I have posted on this group I have always
gotten one or more helpful answers to my questions as well as some
interesting discussions on the side.

Chris N.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 01 Jun 2002 03:00:00 GMT  
 Pointers & incrementing array address

Quote:
>Thanks again!  The few times I have posted on this group I have always
>gotten one or more helpful answers to my questions as well as some
>interesting discussions on the side.

You're welcome

--
HS
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Sat, 01 Jun 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. qsorting & managing struct arrays, pointer arrays

2. address & pointer

3. C Question - pointer & address-operator

4. assign address to pointer array

5. Address of array and function, & is superfluous

6. passing the address of an array of pointers?

7. Long as array/pointer address

8. DOS, pointers & flat addressing Question

9. incrementing addresses

10. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

11. Array of pointers, pointer to array...

12. array pointer/pointer array

 

 
Powered by phpBB® Forum Software