C: array sum using pointers [closed]












-1















How could I get the sum of an array using only pointers ?



int array_sum(int *begin, int *end)
{
int arraysum = 0;
int *p = 0;
for (p = begin; p < end; p ++)
{
arraysum += *(arr + p);
}

return arraysum;
}


this doesn't compile at line



arraysum += *(arr + p);









share|improve this question













closed as off-topic by Fred Larson, chux, 4386427, usr, Govind Parmar Nov 21 '18 at 22:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Fred Larson, chux, 4386427, usr

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1





    Why do you add arr and p? Just use *p.

    – tkausl
    Nov 21 '18 at 21:49











  • So that I can get arr[p] ?

    – cuckcuc
    Nov 21 '18 at 21:49











  • This only works if you're iterating over indicies which you aren't. You're iterating over the memory-locations of the array, so p always points to the current number.

    – tkausl
    Nov 21 '18 at 21:50








  • 3





    What is arr ?

    – John3136
    Nov 21 '18 at 21:51






  • 2





    You need to post all the code. How is the function called? What is arr?

    – 4386427
    Nov 21 '18 at 21:56
















-1















How could I get the sum of an array using only pointers ?



int array_sum(int *begin, int *end)
{
int arraysum = 0;
int *p = 0;
for (p = begin; p < end; p ++)
{
arraysum += *(arr + p);
}

return arraysum;
}


this doesn't compile at line



arraysum += *(arr + p);









share|improve this question













closed as off-topic by Fred Larson, chux, 4386427, usr, Govind Parmar Nov 21 '18 at 22:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Fred Larson, chux, 4386427, usr

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1





    Why do you add arr and p? Just use *p.

    – tkausl
    Nov 21 '18 at 21:49











  • So that I can get arr[p] ?

    – cuckcuc
    Nov 21 '18 at 21:49











  • This only works if you're iterating over indicies which you aren't. You're iterating over the memory-locations of the array, so p always points to the current number.

    – tkausl
    Nov 21 '18 at 21:50








  • 3





    What is arr ?

    – John3136
    Nov 21 '18 at 21:51






  • 2





    You need to post all the code. How is the function called? What is arr?

    – 4386427
    Nov 21 '18 at 21:56














-1












-1








-1


1






How could I get the sum of an array using only pointers ?



int array_sum(int *begin, int *end)
{
int arraysum = 0;
int *p = 0;
for (p = begin; p < end; p ++)
{
arraysum += *(arr + p);
}

return arraysum;
}


this doesn't compile at line



arraysum += *(arr + p);









share|improve this question














How could I get the sum of an array using only pointers ?



int array_sum(int *begin, int *end)
{
int arraysum = 0;
int *p = 0;
for (p = begin; p < end; p ++)
{
arraysum += *(arr + p);
}

return arraysum;
}


this doesn't compile at line



arraysum += *(arr + p);






c arrays sum






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 21:47









cuckcuccuckcuc

13




13




closed as off-topic by Fred Larson, chux, 4386427, usr, Govind Parmar Nov 21 '18 at 22:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Fred Larson, chux, 4386427, usr

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Fred Larson, chux, 4386427, usr, Govind Parmar Nov 21 '18 at 22:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Fred Larson, chux, 4386427, usr

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1





    Why do you add arr and p? Just use *p.

    – tkausl
    Nov 21 '18 at 21:49











  • So that I can get arr[p] ?

    – cuckcuc
    Nov 21 '18 at 21:49











  • This only works if you're iterating over indicies which you aren't. You're iterating over the memory-locations of the array, so p always points to the current number.

    – tkausl
    Nov 21 '18 at 21:50








  • 3





    What is arr ?

    – John3136
    Nov 21 '18 at 21:51






  • 2





    You need to post all the code. How is the function called? What is arr?

    – 4386427
    Nov 21 '18 at 21:56














  • 1





    Why do you add arr and p? Just use *p.

    – tkausl
    Nov 21 '18 at 21:49











  • So that I can get arr[p] ?

    – cuckcuc
    Nov 21 '18 at 21:49











  • This only works if you're iterating over indicies which you aren't. You're iterating over the memory-locations of the array, so p always points to the current number.

    – tkausl
    Nov 21 '18 at 21:50








  • 3





    What is arr ?

    – John3136
    Nov 21 '18 at 21:51






  • 2





    You need to post all the code. How is the function called? What is arr?

    – 4386427
    Nov 21 '18 at 21:56








1




1





Why do you add arr and p? Just use *p.

– tkausl
Nov 21 '18 at 21:49





Why do you add arr and p? Just use *p.

– tkausl
Nov 21 '18 at 21:49













So that I can get arr[p] ?

– cuckcuc
Nov 21 '18 at 21:49





So that I can get arr[p] ?

– cuckcuc
Nov 21 '18 at 21:49













This only works if you're iterating over indicies which you aren't. You're iterating over the memory-locations of the array, so p always points to the current number.

– tkausl
Nov 21 '18 at 21:50







This only works if you're iterating over indicies which you aren't. You're iterating over the memory-locations of the array, so p always points to the current number.

– tkausl
Nov 21 '18 at 21:50






3




3





What is arr ?

– John3136
Nov 21 '18 at 21:51





What is arr ?

– John3136
Nov 21 '18 at 21:51




2




2





You need to post all the code. How is the function called? What is arr?

– 4386427
Nov 21 '18 at 21:56





You need to post all the code. How is the function called? What is arr?

– 4386427
Nov 21 '18 at 21:56












2 Answers
2






active

oldest

votes


















0














You could do something like this.



int array_sum(int *begin, int *end)
{
int arraysum = 0;
while (begin < end)
{
arraysum += *begin;
++begin;
}
return arraysum;
}





share|improve this answer


























  • Ah YES ! I perfectly understand this

    – cuckcuc
    Nov 21 '18 at 21:56



















0














for (p = begin; p < end; p ++)
{
arraysum += *(arr + p);
}


This doesn't work because you can't add arr and p. You don't need to add anything to p, your number is in *p.



As to why: When you iterate over indicies, i.e. 0 to arraylength - 1, you always use the begin pointer and add the index to it to find you number. In your loop however, you copy the beginning pointer to p and increment P directly, so p starts at the first number, once incremented points at the second number, and so on, until p and end are the same, note the p < end as condition.




dont understand how the loop is incremented. we get at first p = begin
(adress number) right ? What does it exactly do at p ++ ? It does p +
1 right ? but adresses of int are of size 4 ???




Pointer-Arithmetic makes this possible. Math on pointers works a bit different, essentially making sure that you add/subtract multiples of the objects size. So adding one to your int-pointer actually adds sizeof(int) under the hood. This is also how array-indicies work, array[i] is equivalent to *(array + i).






share|improve this answer
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could do something like this.



    int array_sum(int *begin, int *end)
    {
    int arraysum = 0;
    while (begin < end)
    {
    arraysum += *begin;
    ++begin;
    }
    return arraysum;
    }





    share|improve this answer


























    • Ah YES ! I perfectly understand this

      – cuckcuc
      Nov 21 '18 at 21:56
















    0














    You could do something like this.



    int array_sum(int *begin, int *end)
    {
    int arraysum = 0;
    while (begin < end)
    {
    arraysum += *begin;
    ++begin;
    }
    return arraysum;
    }





    share|improve this answer


























    • Ah YES ! I perfectly understand this

      – cuckcuc
      Nov 21 '18 at 21:56














    0












    0








    0







    You could do something like this.



    int array_sum(int *begin, int *end)
    {
    int arraysum = 0;
    while (begin < end)
    {
    arraysum += *begin;
    ++begin;
    }
    return arraysum;
    }





    share|improve this answer















    You could do something like this.



    int array_sum(int *begin, int *end)
    {
    int arraysum = 0;
    while (begin < end)
    {
    arraysum += *begin;
    ++begin;
    }
    return arraysum;
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 21:58

























    answered Nov 21 '18 at 21:54









    alamitalamit

    36719




    36719













    • Ah YES ! I perfectly understand this

      – cuckcuc
      Nov 21 '18 at 21:56



















    • Ah YES ! I perfectly understand this

      – cuckcuc
      Nov 21 '18 at 21:56

















    Ah YES ! I perfectly understand this

    – cuckcuc
    Nov 21 '18 at 21:56





    Ah YES ! I perfectly understand this

    – cuckcuc
    Nov 21 '18 at 21:56













    0














    for (p = begin; p < end; p ++)
    {
    arraysum += *(arr + p);
    }


    This doesn't work because you can't add arr and p. You don't need to add anything to p, your number is in *p.



    As to why: When you iterate over indicies, i.e. 0 to arraylength - 1, you always use the begin pointer and add the index to it to find you number. In your loop however, you copy the beginning pointer to p and increment P directly, so p starts at the first number, once incremented points at the second number, and so on, until p and end are the same, note the p < end as condition.




    dont understand how the loop is incremented. we get at first p = begin
    (adress number) right ? What does it exactly do at p ++ ? It does p +
    1 right ? but adresses of int are of size 4 ???




    Pointer-Arithmetic makes this possible. Math on pointers works a bit different, essentially making sure that you add/subtract multiples of the objects size. So adding one to your int-pointer actually adds sizeof(int) under the hood. This is also how array-indicies work, array[i] is equivalent to *(array + i).






    share|improve this answer






























      0














      for (p = begin; p < end; p ++)
      {
      arraysum += *(arr + p);
      }


      This doesn't work because you can't add arr and p. You don't need to add anything to p, your number is in *p.



      As to why: When you iterate over indicies, i.e. 0 to arraylength - 1, you always use the begin pointer and add the index to it to find you number. In your loop however, you copy the beginning pointer to p and increment P directly, so p starts at the first number, once incremented points at the second number, and so on, until p and end are the same, note the p < end as condition.




      dont understand how the loop is incremented. we get at first p = begin
      (adress number) right ? What does it exactly do at p ++ ? It does p +
      1 right ? but adresses of int are of size 4 ???




      Pointer-Arithmetic makes this possible. Math on pointers works a bit different, essentially making sure that you add/subtract multiples of the objects size. So adding one to your int-pointer actually adds sizeof(int) under the hood. This is also how array-indicies work, array[i] is equivalent to *(array + i).






      share|improve this answer




























        0












        0








        0







        for (p = begin; p < end; p ++)
        {
        arraysum += *(arr + p);
        }


        This doesn't work because you can't add arr and p. You don't need to add anything to p, your number is in *p.



        As to why: When you iterate over indicies, i.e. 0 to arraylength - 1, you always use the begin pointer and add the index to it to find you number. In your loop however, you copy the beginning pointer to p and increment P directly, so p starts at the first number, once incremented points at the second number, and so on, until p and end are the same, note the p < end as condition.




        dont understand how the loop is incremented. we get at first p = begin
        (adress number) right ? What does it exactly do at p ++ ? It does p +
        1 right ? but adresses of int are of size 4 ???




        Pointer-Arithmetic makes this possible. Math on pointers works a bit different, essentially making sure that you add/subtract multiples of the objects size. So adding one to your int-pointer actually adds sizeof(int) under the hood. This is also how array-indicies work, array[i] is equivalent to *(array + i).






        share|improve this answer















        for (p = begin; p < end; p ++)
        {
        arraysum += *(arr + p);
        }


        This doesn't work because you can't add arr and p. You don't need to add anything to p, your number is in *p.



        As to why: When you iterate over indicies, i.e. 0 to arraylength - 1, you always use the begin pointer and add the index to it to find you number. In your loop however, you copy the beginning pointer to p and increment P directly, so p starts at the first number, once incremented points at the second number, and so on, until p and end are the same, note the p < end as condition.




        dont understand how the loop is incremented. we get at first p = begin
        (adress number) right ? What does it exactly do at p ++ ? It does p +
        1 right ? but adresses of int are of size 4 ???




        Pointer-Arithmetic makes this possible. Math on pointers works a bit different, essentially making sure that you add/subtract multiples of the objects size. So adding one to your int-pointer actually adds sizeof(int) under the hood. This is also how array-indicies work, array[i] is equivalent to *(array + i).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 22:03

























        answered Nov 21 '18 at 21:54









        tkausltkausl

        8,05912142




        8,05912142















            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen