Can someone demonstrate how to pass by pointers using one function that takes two arguments from two variables as input?
#include <iostream>
using namespace std;
int passPoints(int *x, int *y)
{
int y = *x * *y;
return(y);
}
int *X, *Y;
int main()
{
cout<<"enter two values: ";
cin>>*X;
cin>>*Y;
cout<<"RESULT = "<<passPoints(X,Y);
return 0;
}
Having trouble.
Like this:
int X, Y;
int main()
{
cout<<"enter two values: ";
cin>>X;
cin>>Y;
cout<<"RESULT = "<<passPoints(&X,&Y);
return 0;
}
The biggest problem in your code (if it is not a typo when you are extracting for question) is, int* X and Y are null pointer and the location they are pointing at are not valid for you to update. You need to make sure that these pointers are pointing to a valid memory location, either by:
int x, y;
int* xPtr = &x;
int* yPtr = &y;
passPoints(xPtr, yPtr);
or simply
int x, y;
passPoints(&x, &y);
now the pointers received by passPoints() are valid pointers
Related
I am getting an illegal hardware instruction error when compiled on mac. Appreciate any pointers.
#include<iostream>
using namespace std;
int * fun(int * x)
{
return x;
}
int main()
{
int * x;
*x=10;
cout << fun(x);
return 0;
}
Pointers are just pointers. In your code there is no integer that you could assign a value to.
This
int * x;
Declares x to be a pointer to int. It is uninitialized. It does not point anywhere. In the next line:
*x=10;
You are saying: Go to the memory that x points to and assign a 10 to that int. See the problem? There is no int where x points to, because x doesnt point anywhere. Your code has undefined behavior. Output could be anything.
If you want to assign 10 to an int you need an int first. For example:
#include<iostream>
using namespace std;
int * fun(int * x)
{
return x;
}
int main()
{
int y = 0;
int * x = &y;
*x=10;
cout << fun(x);
return 0;
}
This assigns 10 to y. The cout is still printing the value of x, which is the adress of y. It does not print the value of y. Not sure what you actually wanted.
The problem is that in your program the pointer x is not pointing to any int variable. So first you have to make sure that the pointer x points to an int object as shown below.
You can sovle this as shown below:
int i = 0; //create the int object to which x will point
int *x = &i; //make x point to variable i
*x = 10; //dereference the pointer x and assign 10 to the underlying variable i
cout << *fun(x); //this prints 10
I'm trying to make a program that will allow the user to enter in a heighth and width of a rectangle, and display the area, however every step of my program is done using functions as part of the assignment.
The issue I'm having is that I have the variables assigned and intialized, though I'm not sure how to overwrite them with user inputted data. If I don't initialize the variables at all the program will not run. I was hoping someone could tell me what I'm doing wrong. My code is:
#include <iostream>
using namespace std;
double getWidth(int x);
double getLength(int y);
double getArea(double x, double y, double a);
double displayData(double a);
int main()
{
int x = 0, y = 0, a = 0;
getWidth(x);
getLength(y);
getArea(x, y, a);
displayData(a);
system("pause");
return 0;
}
double getWidth(int x)
{
cout << "Please enter the width: ";
cin >> x;
return x;
}
double getLength(int y)
{
cout << "Please enter the length: ";
cin >> y;
return y;
}
double getArea(double x, double y, double a)
{
a = x*y;
return a;
}
double displayData(double a)
{
cout << a << endl;
return a;
}
There are two ways of passing variables. Method number one is to pass by value. This is the most common method, and it is the one your program is doing. In this method, a copy of the data in the variable is being made and supplied to the function. Your function only changes the copy and not the original variable.
The second method is to pass by reference. When passing by reference, your function effectively has a pointer to the original variable and can thus change it. To pass by reference, put in an ampersand (&) in front of the variable in the function header. Note in the code below that it is not necessary to pass x and y to getArea by reference because getArea only needs to read these variables not write to them.
This however will introduce a new problem for you. When you pass by value it is possible to change the variable type to a larger type without an explicit cast. This is not possible with passing by reference because the different parts of the program would then be trying to treat the variable as a different type. i.e. main wants to write to/read from a as if it is an integer and getArea wants to write to/read from a as if it is a double. These two data types have different sizes and and different formats so this is not possible. Thus you have to declare a is a double in main.
#include <iostream>
using namespace std;
double getWidth(int &x);
double getLength(int &y);
double getArea(double x, double y, double &a);
double displayData(double a);
int main()
{
int x = 0, y = 0;
double a;
getWidth(x);
getLength(y);
getArea(x, y, a);
displayData(a);
system("pause");
return 0;
}
double getWidth(int &x)
{
cout << "Please enter the width: ";
cin >> x;
return x;
}
double getLength(int &y)
{
cout << "Please enter the length: ";
cin >> y;
return y;
}
double getArea(double x, double y, double &a)
{
a = x*y;
return a;
}
double displayData(double a)
{
cout << a << endl;
return a;
}
You seem to be confusing a few different concepts. You should either be passing references and assigning them within the functions, or passing less values and assigning them to some variable in main. For example, your getWidth should be:
double getWidth() {
double w;
cin >> w;
return w;
}
and in your main you should have:
int main() {
/* ... */
double width = getWidth();
/* ... */
}
And so on for the others as well. You should be looking into references and pointers in C++ as well, that would be the other way that you could do this (and you are seemingly confused about). Finally you should definitely find an introduction to functions in some intro to C++ book, as someone said above.
I have got stuck by the C++ two dimensional dynamic array. I want to get the array length. Here is the code:
#include <iostream>
using namespace std;
int dosomestuff(char **dict);
int main(){
int x, y;
char **dict;
cin>>x>>y; // here to input the 'x'
dict = new char *[x];
for(i = 0; i < x; i++){
dict[i] = new char[y];
for(j = 0; j < y; j++){
cin>>dict[i][j];
}
}
dosomestuff(dict);
}
int dosomestuff(char **dict){
int x, y;
x = sizeof(*dict); //8 not equal to the 'x'
//run in mac_64 I think this is the pointer's length
y = strlen(dict[0]); //this equal to the 'y' in function main
cout<<x<<" "<<y<<endl;
return 0;
}
What I want is in function dosomestuff to get the x equal the 'x' in function main.
How can I to get it? Can anybody help me ~? Thx a lot.
sizeof(*dict) only gives you the sizeof(char*), which is not what you are hoping for.
There is no way of knowing the value of x from dict in dosomestuff. If you want to use char** for dict, your best option is to pass x and y to dosomestuff.
int dosomestuff(char **dict, int x, int y);
Since you are using C++, you can use:
std::vector<std::string> dict;
Then you would have all the information you need in dosomestuff if you pass that dict to it.
int main()
{
int x = 2, y = 4;
func(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
void func(int *x, int *y)
{
int *temp;
temp = x;
x = y;
y = x;
}
Hi
For this code i have no idea why the output is 2 4 instead of 4 4. Since x = y in func() means x now points to the address of y, and y = x in func() means y now points to the address of x (which is y), both variables are now pointing to y already.
Many thanks!
The answer is simple: You change the pointer values inside func - but not the values they point at. I guess you'd like to swap the variables (due to the temp var).
This code should work (swapping values):
void func(int *x, int *y)
{
int temp = *x; // dereferencing the pointer to get the value it points at.
*x = *y;
*y = temp;
}
Too keep your initial expectations (which don't make any sense code wise due to the second assignment):
void func(int *x, int *y)
{
*x = *y;
*y = *x;
}
Nope, func() will receive copies of those addresses and this won't affect the variables outside the function - all changes done to variables local to func() will be discarded once func() exits.
You are just temporarily assigning the address of 'x' to the address of 'y' within func. In order to make the assignment, you need to dereference your pointers.
*x = *y;
*y = *x;
Try this
#include <iostream>
using namespace std;
void func(int *x, int *y);
int main()
{
int x = 2, y = 4;
func(&x, &y);
//printf("%d %d\n", x, y);
cout << "X - " << x << endl;
cout << "Y - " << y << endl;
return 0;
}
void func(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
You're making copies of the passed in pointers, they are local to your function. Any change you make to them doesn't affect the outside. You should capture the pointers by reference &.
void func(int*& x, int*& y){
// as before...
}
Now the changes inside the function will be correctly reflected outside of it. On problem remains though. You're passing in the address of local variables, and attempt to change their pointers - that doesn't work. When you take the address of a variable &x, you make a new temporary pointer, which can't be converted to a reference-to-pointer.
Do this instead:
int main(){
int x = 2, y = 4;
int *px = &x, *py = &y;
func(px, py);
printf("%d %d\n",*px,*py);
}
Edit: If you instead want to swap / set the values of x and y and not just some pointers, do as the other answers state.
The problem is that you're trying to write your own version of std::swap. This will work:
int x = 2, y = 4;
std::swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
using namespace std;
int addition (int a, int b)
{
return (a+b);
}
int subtraction (int a, int b)
{
return (a-b);
}
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return(g);
}
int main()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7,5,addition);
n = operation (20,m,minus);
cout << n;
return 0;
}
Can anybody explain this line for me
int (*minus)(int,int) = subtraction;
Thanks a lot!
int (*minus)(int,int) = subtraction; is creating a variable called minus and assigning it a pointer to the function called subtraction. if the code is valid then the function subtraction would be declared int subtraction(int a, int b);.
the best way to deal with function pointers is to make them readable using typedef.
example:
typedef int (*math_op)(int,int); // new types is math_op
int subtraction (int a, int b)
{
return (a-b);
}
math_op minus = subtraction;
later on these can be called like they are normal functions.
example:
int result = minus(10, 2); // result is now set to 8
your code rewritten:
using namespace std;
typedef int (*math_op)(int,int); // new types is math_op
int addition (int a, int b)
{
return (a+b);
}
int subtraction (int a, int b)
{
return (a-b);
}
int operation (int x, int y, math_op functocall)
{
int g;
g = functocall(x,y);
return(g);
}
int main()
{
int m,n;
math_op minus = subtraction;
m = operation (7,5,addition);
n = operation (20,m,minus);
cout << n;
return 0;
}
"minus" is a name of a variable which is a pointer to a function taking two int arguments and returning another int.
The function called "operation" takes 3 arguments: 2 ints and a pointer to a function which operates on 2 ints and return another one. When invoked, the operation function applies argument 3 to the arguments 1 and 2.
int (*minus)(int,int)
says
A pointer to a function taking two ints as arguments returning an int.
The parentheses around (*minus) are there to make sure that the asterisk binds to the name of the typedef and not the return type (i.e., the function does not return an int*).