Solved

Adding integer data with single pointer

Posted on 2006-11-09
5
232 Views
Last Modified: 2010-04-15
If you have an array of integers, is it legal (or recommended) to get a sum by iterating through the array with a pointer, and adding the value pointed to by the next pointer, to the value pointed to by the current pointer?  Like this:

      int* ptr = (int*) malloc(3 * sizeof(int));

      ptr[0] = 66;
      ptr[1] = 50;
      ptr[2] = 1000;

      int* sum = ptr;
      for (unsigned i = 0; i < 2; ++i)
            *sum += *(++sum);

      printf("%d\n",*sum);

This works, but I wonder if it's ANSI compliant, or if it is even a good idea.  What sort of assembly code would this generate?  Would it be more efficient than using a temporary value and incrementing that?
0
Comment
Question by:chsalvia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17911892
Hi chsalvia,

No it would not work ...
*sum += *(++sum);
This modifies the value *sum more than once between two sequence points ...
http://publications.gbdirect.co.uk/c_book/chapter8/sequence_points.html

Cheers!
sunnycoder
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 17911913
oops .. I posted too soon ... ++sum modifies the pointer and += modifies the *sum, so this should be fine ... note that you are chaging the values in your array to get cumulative sum
0
 

Author Comment

by:chsalvia
ID: 17911922
I see.  So you'd need to do:

      int sum = 0;
      for (unsigned i = 0; i < 3; ++i)
            sum += *(ptr++);
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17911929
yes, that would not modify the contents of the array and yet give you the sum.
0
 
LVL 84

Expert Comment

by:ozo
ID: 17912065
*sum += *(++sum); modifies sum only once, but it violates the requirement that the prior value shall be read only to determine the value to be stored, so it's still illegal
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question