Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculateSumOfArray(float *table)

Status
Not open for further replies.

rastkocvetkovic

Programmer
Joined
Aug 11, 2002
Messages
63
Location
SI
What's wrong with my function. It hangs upon usage. Thank you for help!

float calculateSumOfArray(float *table) {
int index;
index = 0;
float sum;
sum = 0;
while ((table+index) != NULL) {
sum = sum + *(table+index);
index++;
}
return sum;
}
 
The termination condition ((table+index) != NULL) is suspect. The address of the end of an array isn't NULL. One solution is to pass in the size of the array to your function

float calculateSumOfArray(float *table, int entries) {
int index;
float sum;
sum = 0;
for (index = 0; index < entries; index++) {
sum = sum + *(table+index);
}
return sum;
}
 
Status
Not open for further replies.

Similar threads

Replies
4
Views
256
Replies
3
Views
244
  • Locked
  • Question Question
Replies
2
Views
164
  • Locked
  • Question Question
Replies
4
Views
262

Part and Inventory Search

Sponsor

Back
Top