Today i was learning and testing different forms of using switch statement in C++; then i wrote this code to make a function that allows user to input three characters that two are the cases and one is the comparing character(Main input). I decided to use pointers since i couldn't use variables for the cases, but my approach didn't work and i just don't understand why? cause using pointers actually means that i'm pointing at the value of the address which already has been defined!
This is the errors:
[Error] 'iloc' cannot appear in a constant-expression
[Error] '*' cannot appear in a constant-expression
This is the code:
#include <iostream>
using std::cout;
using std::cin;
void switch_function(char i, char j, char c){
//inputing values by the user
cout <<"Insert i(char): ";
cin >> i;
cout <<"Insert j(char): ";
cin >> j;
cout <<"Insert c(char) "<< i <<" Or "<< j <<": ";
cin >> c;
//declaring pointers
char * iloc;
char * jloc;
char * cloc;
//registering memory adresses
iloc = &i;
jloc = &j;
cloc = &c;
//switch function
switch(*cloc){
case *iloc:
cout << i;
break;
case *jloc:
cout << j;
break;
}
}
int main(){
//s and f characters are the cases and the third f is the main user input.
switch_function('s', 'f', 'f');
cout <<"\n";
int location;
int * target;
target = &location;
cout << &location;
cout <<"\n"<< target + 1;
}