Exercise about pointer to function in C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an exercise like that. The requirement is to write code in the section
#include <iostream>
int minus (int a, int b) {
return a - b;
}
void calculate(int a, int b)
{
int (*myfunc)(int, int);
// STUDENT ANSWER BEGIN
// STUDENT ANSWER END
int ans = (*myfunc)(a, b);
printf("Output of calculation is %d.\n", ans);
}
int main() {
calculate(1,2);
return 0;
}
When I write
int (*myfunc) (int, int) = add;
int temp = (*myfunc)(a, b);
cout << temp;
the value of temp = -1 as expected (just for test) but I can't get the value of ans. So, I figure that two *(myfunc) is different. How can I fix that?
Thank you very much!

When you write:
int (*myfunc) (int, int) = add;
you are creating a new variable named myfunc. If you do it inside the same scope as the original myfunc, it is a compilation error, but if you write it inside a new scope (which the indentation of the code you pasted suggest, even if you haven't showed the brackets), you are shadowing the original variable with the new one. By using the name myfunc inside that scope, you are referring to the new variable.
The solution is simple. Instead of declaring a new variable, simply assign to the existing one:
myfunc = add; // or minus
EDIT: I misread the question and interpreted "but I can't get the value of ans" as "but I can't get the value into ans". But I'll leave this answer here in case that is what OP meant...

Related

Is it okay if we assign const integer a value like this? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I wanted to assign a value to a constant but as it is constant, we cant assign value to it through cin. So I came up with this idea. Is it okay if we use it like this?
{
int data = 0;
cout << "enter number of students"<<endl;
cin>>data;
const int number = data;
cout << number<<endl;
}
This is fine. const values do not need to be compile time constants. They just can't be changed after they are defined.
Similarly you could do:
int getNumberFromStdin() {
int data;
cin >> data;
return data;
}
int main() {
const int number = getNumberFromStdin();
}
Yes, because it is not an assignment; it is a declaration.
Despite looking quite similar, the two constructs are different: assignment changes something that is already declared, while the declaration sets up something new, and optionally gives it a value. The value does not need to be hard-coded, though: it can be a result of some processing, as is the case in your example.
Naturally, assignment is not allowed for constants, because they already have a value.

Variable declare & Assign value in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to declare a variable without assigning value in C++?
int a; int a=15; Which one will be Correct?
If i assign value to a variable 3 times or more which one will count at the end in C++??
int a=15;
int a=10;
int a=5;
Which value will be execute for a at the end?
int a; // declared but not assigned
a = 1; // assigning a value
a = 2; // assigning a different value
a = 3; // assigning another value
std::cout << a << "\n"; // will print 3 since only the last assignment matters
int x; declares a variable x without assigning a value.
If you assign to a variable three times then which ever assignment executed last will be the variables final value. Very important idea, C++ statements execute in a specific order, and which order they execute in is essential to understanding what a program does.

Why is this recursive function creating an infinite loop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
The following code results in an infinite loop. The value of a does not seem to be going lower than 1. Please help.
int main()
{
init(4);
return 0;
}
void init(int a)
{
int j;
cout<<"After declaring: "<<j;
j = a;
cout<<"After initializing: "<<j;
a--;
while(a>0)
{
init(a);
}
}
First, you are accessing an uninitialized variable. This introduces undefined behaviour into your program; I'd remove this section.
Second, your while-loop runs as long as a > 0, but in the body of the loop, the value of a is never changed. Note that when calling init, parameter a is passed by value, i.e. the value of a is copied; Each instance of init will get it's own a, and it will never change the value of the caller's a.
Recursive functions usually are used instead of a loop. Nevertheless, you need an "anchor" to tell the recursion where to stop:
void init(int a)
{
int j = a;
cout<<"After initializing: " << j;
if (a>0) {
init (a-1);
}
}

function returns local variable, though variable is out of scope no compiler issues and code executes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
My function is supposed to return a local variable. It manages to do this without any compiler issues even though variable is out of scope.
int add(int a, int b);
{
int result=0;
result = a + b;
return (result); // result scope should be limited to this method
}
int main()
{
int res=0;
res = (3 + 4);
printf("Result : %d \n", res);
return 0;
}
Can anyone please explain this behavior.
When you do
return (result);
result is returned by value. So, the caller gets a copy of the value in result, and uses this copy subsequently. result itself goes out of scope, and is not accessed again.
If your variable was instead a pointer, it would be incorrect. You can read more about it from this question.
In addition, you seem to have forgotten to use add() at all. I suppose you meant to use
res = add(3,4);
in main() instead of
res = (3 + 4);

program to view addresses of elements in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
help plz its showing invalid indirection
i used it to find the location or memory addresses of elements in the array b
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr;
int b[]={1,0,2,3,4,5,6,7,8,9};
ptr=b;
for(int i=0;i<10;i++)
cout<<ptr[i]<<" "<<*b[i];
}
In order to print the address of the ith element in an array b, use
std::cout << b + i;
This will work in all cases except when b is an array of char, in which case you need to cast to void*
std::cout << static_cast<const void*>(b + i);
in place of iostream.h it should be iostream.
void main() ; it should be int main().
cout<<ptr[i]<<" "<<(b+i)<<endl;
Formatting by using endl in above line of code will make the result clear.
Your function should return an integer value.
return 0;