1

So currently I am still confused in using pointers and reference and I do not know if what I am doing is right. My program is a calculator that stores everything the user inputs. There is an option that the user will be asked if he/she want to view the history and if he/she wants it the program will show all the data he/she inputs.And I need to use pointers and reference in my program but I am still confused on how to use pointers and reference into array Here is my initialization:

int main() {
  int size = 1, fNum[size], sNum[size];
  char oprtn[size], answer;
  ;
  float result[size];
  int *ptrf = &fNum[size];
  int *ptrs = &sNum[size];
  char *ptro = &oprtn[size];
  float *ptrRes = &result[size];
  while (true) {
    cout << "=====CALCULATOR=====\n\n";
    cout << "ENTER TWO NUMBERS:" << endl;
    while (!(cin >> *ptrf >> *ptrs)) {
      system("cls");
      cout << "INVALID INPUT. PLEASE ENTER TWO NUMBERS:\n";
      cin.clear();
      cin.ignore(2);
    }
    cout << endl;
    do {
      cout << "Choose Operation to be Used: \n"
           << "   +   --- Addition  \n"
           << "   -   --- Subtraction   \n"
           << "   *   --- Multiplication   \n"
           << "   /   --- Division   \n"
           << "   %   --- Remainder   \n";
      cout << "Answer: ";
      cin >> answer;
      cout << endl;
      switch (answer) {
        case '+':
          cout << "ADDITION\n";
          break;
        case '-':
          cout << "SUBTRACTION\n";
          break;
        case '*':
          cout << "MULTIPLICATION\n";
          break;
        case '/':
          cout << "DIVISION\n";
          break;
        case '%':
          cout << "REMAINDER\n";
          break;
        default:
          answer = false;
          system("cls");
          cout << "PLEASE ENTER A VALID ANSWER. CHOOSE BELOW.\n\n";
          cout << "FIRST NUMBER: " << *ptrf << endl;
          cout << "SECOND NUMBER: " << *ptrs;
          cout << endl << endl;
          continue;
      }
    } while (!answer);
    cout << "DO YOU WANT TO TRY AGAIN? (Y / N): ";
    cin >> answer;
    switch (answer) {
      case 'Y':
      case 'y':
        system("cls");
        continue;
      default:
        cout << "VIEW HISTORY? (Y / N): ";
        cin >> answer;
        switch (answer) {
          case 'Y':
          case 'y':
            cout << "HISTORY\n\n";
            break;
          default:
            return 0;
        }
    }
  }
}
18
  • 6
    @MichaelChourdakis An array is not a pointer. It can be used as a pointer when used as a rvalue. The C/C++ wording is that an array can decay to a pointer. Commented Mar 4, 2020 at 12:20
  • 1
    btw your size is 1 so all your arrays are really just a single number Commented Mar 4, 2020 at 12:22
  • 3
    @MichaelChourdakis isnt it the misunderstanding that arrays would be pointers that causes the confusion among newbies? Commented Mar 4, 2020 at 12:26
  • 1
    you increment size (I dont see it in the code) but that wont change the size of the arrays. They are still arrays of size 1 Commented Mar 4, 2020 at 12:26
  • 1
    "yes the size is 1 but i will add an increment on the size every time the user want to view history": it doesn' work like this. You cannot change the size of a raw array. Use std::vector instead. Commented Mar 4, 2020 at 12:45

2 Answers 2

2

This is not valid C++ code:

  int size = 1, fNum[size], sNum[size];   // wrong: C++ forbids Variable Length Arrays (1)
  char oprtn[size], answer;               // ditto...
  ;
  float result[size];                     // ditto...
  int *ptrf = &fNum[size];         // wrong: this syntax makes ptrf points one past end of array (2)
  int *ptrs = &sNum[size];         // ditto...
  char *ptro = &oprtn[size];       // ditto...

(1): VLA are a C language concept. Some compilers (gcc and CLang) allow it as an extension but it is useless in C++ because of the containers from the standard library

(2): the idiomatic way to initialize a pointer to the beginning of an array is just int *ptrf = Num; When used as a rvalue (in short at the right side of an = sign), an array decays to a pointer to its first element. So it reads (int *) ptr = &(fNum[0]);: ptr is a pointer to int and its initial value is the address of the first element of the array fNum

Sign up to request clarification or add additional context in comments.

2 Comments

Nitpick: int *ptrf = &fNum[size] declares a valid but not dereferencable pointer
@Caleth: The problem is not for ptrf to be a valid pointer. It is that OP wanted to initialize it to point to the list...
1
fNum[size] 

is an array of one integer.

int *ptrf = &fNum[size];

Is a pointer to one past the last element of this array, therefore it's out of bounds access, this is undefined behaviour.

Since it only has one element, you declaring it as an array is pointless.

A pointer to the beginning of an array would be:

int *ptrf = fNum;

Or

int *ptrf = &fNum[0];

You can then cycle trough the array incrementing the pointer ptrf++.

To assing a pointer to a variable:

int x;
int *ptr = &x;

So the variable declarations and assignments:

int size = 1, fNum[size], sNum[size];
char oprtn[size], answer;
float result[size];
int *ptrf = &fNum[size];
int *ptrs = &sNum[size];
char *ptro = &oprtn[size];
float *ptrRes = &result[size];

Are the same as:

int fNum, sNum;
char oprtn, answer;;
float result; 
int *ptrf = &fNum;
int *ptrs = &sNum;
char *ptro = &oprtn;
float *ptrRes = &result;

That said, C++ has mutch nicer data containers you can use like std::vector or std::array.

One last note, variable length arrays(fNum[size]) are forbidden in C++.

15 Comments

so, incrementing the address of a variable is the same as incrementing 'i' in 'array[i]'?
int *ptrf = &fNum[size]; is not a pointer to the last element, that would be int *ptrf = &fNum[size - 1];
@newbie incrementing a pointer to an address of a given array will make the pointer point to the address of the next element of said array, and yes it has the same effect of incrementing i in array[i].
so in that case, having a pointer to a n array is useless? like 'int *ptrf = &fNum[size]' ?
@newbie, it's not useless, this is what pointers are for, like many things in C, there is more than one way to do somenthing, I say C because in C++ the need to use raw pointers is greatly reduced. In C, an example of that need is, for instance, if you want to pass the address of a variable to a function by parameter so it can be modified in the function you must use pointers, otherwise you will be passing copies of the variables.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.