rgb192
asked on
integerArray is a pointer
// ArrayDemo - demonstrate the use of arrays
// by reading a sequence of integers
// and then displaying them and their sum
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// prototype declarations
int readArray(int integerArray[], int maxNumElements);
int sumArray(int integerArray[], int numElements);
void displayArray(int integerArray[], int numElements);
int main(int nNumberofArgs, char* pszArgs[])
{
// input the loop count
cout << "This program sums values entered "
<< "by the user\n";
cout << "Terminate the loop by entering "
<< "a negative number\n";
cout << endl;
// read numbers to be summed from the user into a
// local array
int inputValues[128];
int numberOfValues = readArray(inputValues, 128);
// now output the values and the sum of the values
displayArray(inputValues, numberOfValues);
cout << "The sum is "
<< sumArray(inputValues, numberOfValues)
<< endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
cout << "Press Enter to continue..." << endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
// readArray - read integers from the operator into
// 'integerArray' until operator enters neg.
// Return the number of elements stored.
int readArray(int integerArray[], int maxNumElements)
{
int numberOfValues;
for(numberOfValues = 0;
numberOfValues < maxNumElements;
numberOfValues++)
{
// fetch another number
int integerValue;
cout << "Enter next number: ";
cin >> integerValue;
// if it's negative...
if (integerValue < 0)
{
// ...then exit
break;
}
// ... otherwise store the number
// into the storage array
integerArray[numberOfValues] = integerValue;
}
// return the number of elements read
return numberOfValues;
}
// displayArray - display the members of an
// array of length sizeOfloatArray
void displayArray(int integerArray[], int numElements)
{
cout << "The value of the array is:" << endl;
for (int i = 0; i < numElements; i++)
{
cout << i << ": " << integerArray[i] << endl;
}
cout << endl;
}
// sumArray - return the sum of the members of an
// integer array
int sumArray(int integerArray[], int numElements)
{
int accumulator = 0;
for (int i = 0; i < numElements; i++)
{
accumulator += integerArray[i];
}
return accumulator;
}
integerArray 0x0034fb28 {3} int *
maintains the first integerValue
I do not understand that - integerArray 0x0034fb28 {3} int *
is a pointer
why is this always the first value (3 in this example)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
first value to save space on old computers
thanks for historical data also
thanks for historical data also
Open in new window
no other language uses the concept and one of the first enhancements c++ added to c was to provide container classes which were a full and safe substitute for the pointer-array mess of c. the idea to turn an array variable to a simple pointer when passing it to a function, is one of the biggest causes for troubles in millions of programs. don't think that there are any efficiency benefits involved by that as for any kind of dynamic arrays you would need to pass array size or number of elements anyhow.hence, it is good and recommendable praxis - especially for c programmers - to always use a pair of pointer to first element and number of elements when passing arrays to functions where they were turned to pointers or use a structure instead of a plain array.
Sara