Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an input method in C++ with pointers

I want to create an input method that creates two ints with Pointers. It would be nice if you could help me or give me any tips. :)

my method:

void inputTest(int* x, int* y) {
    cin >> x;
    cin >> y;
}

my main:

int *x = 0;
int *y = 0;
cout << "Input: " << endl;


//set input from user x,y with input method 
inputTest(x,y);
like image 977
Coder95 Avatar asked Nov 21 '19 15:11

Coder95


People also ask

How do you take inputs to pointers?

int* x = 0 creates a pointer to location 0 in memory. Instead, we want to allocate memory, and then point to that memory. We can initialize x as int x = 0 and then get a pointer to it by using &x. int x = 0; int y = 0; cout << "Input: " << endl; inputTest(&x,&y);

How do you use a pointer to a function?

You can use a trailing return type in the declaration or definition of a pointer to a function. For example: auto(*fp)()->int; In this example, fp is a pointer to a function that returns int .

How do you declare a pointer in C?

The * symbol indicates that the variable is a pointer. To declare a variable as a pointer, you must prefix it with *. In the example above, we have done a pointer declaration and named ptr1 with the data type integer.

What is pointer with example in C?

A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.


3 Answers

First of all, you want to read ints, not int*s, so you need to dereference the pointers:

void inputTest(int* x, int* y) {
    cin >> *x;
    cin >> *y;
}

Then you need to pass valid pointers to the function - yours are null pointers and point nowhere at all.
The best way to do this is to first create two ints and then acquire their locations with the "address-of" operator, &.

int x = 0;
int y = 0;
cout << "Input: " << endl;
inputTest(&x, &y);
like image 58
molbdnilo Avatar answered Nov 15 '22 00:11

molbdnilo


I'm new to pointers so I want to try it that way.. :)

Ok, then first lesson: Do not use pointers when you don't have to. Pointers can cause the most nasty bugs that you dont get without them.

Next: Pointers are just pointers. The can point to something. A int* can point to an int. Your pointers do not point to anything meaningful.

To store integer values you need ints somewhere. Having pointers pointing somewhere is not sufficient. Once you have a int, eg int x; then &x will give you a int* namely the address of x (& is called the address-of operator, but dont get confused, & can have a different meaning, see below). If you have the pointer, int* p = &x; then you can dereference the pointer to get back x: *p = 5; will set the value of x to 5. Using that you could write

void inputTest(int* x, int* y) {
    std::cin >> *x;    
    std::cin >> *y;
}
int main() {
   int x,y;
   inputTest(&x,&y);
   std::cout << x << " " << y;
}

BUT (would like to make it even more bold, because it really is a big "but"). There is an alternative and this is what you should use here. Pointers as parameters are useful when "not pointing anywhere" is an allowed parameter. For a fucntion that wants to read input from user and store that somewhere an invalid pointer is of little use. Better is to disallow such invalid input and use references:

void inputTest(int& x, int& y) {
    std::cin >> x;    
    std::cin >> y;
}
int main() {
   int x,y;
   inputTest(x,y);
   std::cout << x << " " << y;
}

I feel a bit bad for writing this answer, because when you are completely new to pointers, reading an answer here will not be enough to get a proper understanding. Get a book and read it.

like image 43
463035818_is_not_a_number Avatar answered Nov 14 '22 22:11

463035818_is_not_a_number


You need to dereference the pointer in order to assign a value to the pointed location in memory.

void inputTest(int* xptr, int* yptr) {
    cin >> *xptr;
    cin >> *yptr;
}

int* x = 0 creates a pointer to location 0 in memory. Instead, we want to allocate memory, and then point to that memory. We can initialize x as int x = 0 and then get a pointer to it by using &x.

int x = 0;
int y = 0;
cout << "Input: " << endl;
inputTest(&x,&y);

You can also use references...

void inputTest(int& x, int& y) {
    cin >> x;
    cin >> y;
}

int x = 0;
int y = 0;
cout << "Input: " << endl;
inputTest(x,y);
like image 33
Kyy13 Avatar answered Nov 15 '22 00:11

Kyy13