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 strongm 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
Aug 11, 2002
63
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

Part and Inventory Search

Sponsor

Back
Top