i was given an assignment by my prof. , but he stated that using pointer notation . the object t=of the program was to write a C++ function ReverseArray using "pointer notation" that will write out the elements of an array of int in reverse order.
his comments were
This is array notation: cout << "The int values you entered are: " << endl; for (int i = 0; i < arraySize; i++) cout << intList << endl; This is pointer notation cout <<*intList<

i have no clue of what he means so im a bit lost , but he returned my program(F) to fix and trying to get someone to explain what i did wrong


this is my code


#include <iostream>

using namespace std;


int *numList; // array of POINTERS to numbers
int arraySize = 0 ; // input area for longest possible number.

int main()
{
void ReverseArray(int* &intArray, int size);





cout << "Enter the size of the new int array: ";
cin >> arraySize;

cout << "\n"; //newline

//(array var) = new (type)[(size taken from user}]

numList = new int[arraySize];

for(int i = 0; i < arraySize; i++)
{
cout << "\nEnter the next number in the array: ";
cin >> numList[i];


}

ReverseArray(numList, arraySize);


return 0;
}//end main

void ReverseArray(int* &intArray, int size)
{
//for each element up to "size"
for (int i=size-1; i>=0; i--)
/*for(int i = 0; i < size; i++)*/
{
cout << "Element " << i << " equals: " << intArray[i] << "." << endl;
cout << numList[i] << endl; // print the numbers
}

system("PAUSE");
}

Pointer notation is slightly different than conventional array notation. It's just a syntactic issue, so I'm not afraid of doing some of your homework for you:

void ReverseArray(int* &intArray, int size)
{
//for each element up to "size"
for (int i=size-1; i>=0; i--)
{
cout << "Element " << i << " equals: " << *(intArray + i) << "." << endl;
cout << *(numList + i) << endl; // print the numbers
}

system("PAUSE");  //dont ever use this line again
//use this:
cin.ignore(cin.rdbuf()->in_avail());
cin.get();
}

Basically what it means is this:

The * implies you are getting the actual value pointed to by a pointer. Note, an array is just a list of pointers. Therefore putting *ArrayName would give you the value of the first value in the array, since the array name itself points to the first element in the array. *(ArrayName + Integer) will give you the same thing as ArrayName[Integer], since it's returning Integer number of memory locations after the start of the array. The brackets are important! *ArrayName + Integer would give you the first value of arrayName added to the value of the integer (if that's even possible! it depends on the datatype of the array). Hopefully that helps you.

Pointer notation is slightly different than conventional array notation. It's just a syntactic issue, so I'm not afraid of doing some of your homework for you:

void ReverseArray(int* &intArray, int size)
{
//for each element up to "size"
for (int i=size-1; i>=0; i--)
{
cout << "Element " << i << " equals: " << *(intArray + i) << "." << endl;
cout << *(numList + i) << endl; // print the numbers
}

system("PAUSE");  //dont ever use this line again
//use this:
cin.ignore(cin.rdbuf()->in_avail());
cin.get();
}

Basically what it means is this:

The * implies you are getting the actual value pointed to by a pointer. Note, an array is just a list of pointers. Therefore putting *ArrayName would give you the value of the first value in the array, since the array name itself points to the first element in the array. *(ArrayName + Integer) will give you the same thing as ArrayName[Integer], since it's returning Integer number of memory locations after the start of the array. The brackets are important! *ArrayName + Integer would give you the first value of arrayName added to the value of the integer (if that's even possible! it depends on the datatype of the array). Hopefully that helps you.

ok can you clarify this ( i think i understand ) so they are the same

this
int arraySize = 0
also means :
int *arraysize[10]


so if i want it in reverse i would start with the number example arraysize[10] and it would start from reverse ?

>cin.ignore(cin.rdbuf()->in_avail());
FYI, this isn't guaranteed to work. A generally accepted solution is to ignore up to the size of the streambuf or until a newline is detected:

cin.ignore ( numeric_limits<streamsize>::max(), '\n' );

>cin.ignore(cin.rdbuf()->in_avail());
FYI, this isn't guaranteed to work. A generally accepted solution is to ignore up to the size of the streambuf or until a newline is detected:

cin.ignore ( numeric_limits<streamsize>::max(), '\n' );

Interesting...I've never seen it not work. Except if there is a stream error flag raised, in which case it should be preceeded by cin.clear(); . I challenge you to give me a reasonable situation where it doesn't work (besides that one).

And @ shadowfire:
that is not correct.
You have declared an array of 10 int pointers, and declared a single (non pointer) integer.

int iArray[10] = {0};

Now these 2 are true:

*iArray == iArray[0] and *(iArray + 9) == iArray[9]

im trying to learning >>im just try to comprehend.. online learn is the same than in a class room (kinda suks )

>I've never seen it not work.
So you assumed it always works? :icon_rolleyes:

>I challenge you to give me a reasonable situation where it doesn't work
in_avail only tells you how many characters are in the stream object's buffer. If you only clear the buffer, but there are more characters waiting to be loaded into the buffer, ignoring the result of in_avail will fail to do what you want.

It's trivial to test this as well:

#include <iostream>

int main()
{
  std::cout<<"Starting test...";
  std::cin.get();
  std::cin.ignore ( std::cin.rdbuf()->in_avail() );
  std::cin.get();
  std::cout<<"Done.";
}

After a certain number of characters (the size of cin's buffer), the get won't block anymore because the buffer refills from what's still left over. That's because you're relying on the buffer count, not the actual number of streamed characters.

Whether you consider that a "reasonable" situation or not (reasonable is such a subjective word), it's a legitimate flaw in your code, especially when we don't know the size of the buffer on our systems, or if there even is a buffer being used.

>I've never seen it not work.
So you assumed it always works? :icon_rolleyes:

>I challenge you to give me a reasonable situation where it doesn't work
in_avail only tells you how many characters are in the stream object's buffer. If you only clear the buffer, but there are more characters waiting to be loaded into the buffer, ignoring the result of in_avail will fail to do what you want.

It's trivial to test this as well:

#include <iostream>

int main()
{
  std::cout<<"Starting test...";
  std::cin.get();
  std::cin.ignore ( std::cin.rdbuf()->in_avail() );
  std::cin.get();
  std::cout<<"Done.";
}

After a certain number of characters (the size of cin's buffer), the get won't block anymore because the buffer refills from what's still left over. That's because you're relying on the buffer count, not the actual number of streamed characters.

Whether you consider that a "reasonable" situation or not (reasonable is such a subjective word), it's a legitimate flaw in your code, especially when we don't know the size of the buffer on our systems, or if there even is a buffer being used.

Heh. Well I'd say that's reasonable. Thanks. <shruggs off arrogance of first part of reply>

This article has been dead for over six months. Start a new discussion instead.