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;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I know what overloaded functions are but I don’t know how to elaborate it using pointers. If someone can give me a basic program to elaborate function overloading using pointer as a function argument.
As the comments stated, it's not entirely clear where your problem is. It would be nice if you included an example for what you want to understand better, anyway, here's an example:
#include <iostream>
void foo(int x) {
std::cout << x << std::endl;
}
void foo(int* x) {
std::cout << (*x + 1) << std::endl;
}
int main() {
int x = 4;
foo(x); // prints 4
foo(&x); // prints 5
return 0;
}
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...
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
I thought the output would be 70? (20+20+20+10=70) Why is it so large?
#include <iostream>
using namespace std;
int main()
{
int a,b,c=20;
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
The issue is that you are not initializing the variables a and b. That means when you attempt to run your program, the computer is looking in memory for a value to use for each, and that number could be very big or very small. Try this:
#include <iostream>
using namespace std;
int main()
{
int a = 20,b = 20,c=20; //here, a and b are defined
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
C is the only one variable that you initialize to 20, the other 2 variables
(a and b) are holding garbage..
so your math calculation is undefined behaviour.
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
In my code i am trying to have the members of an int pointer-array point to the members of the int array in my struct Bucket. The problem i get is that when running the program, i can copy the first 3 members, but as i try to copy the 4th, i get a runtime error. Why is this? Am I setting the pointers right? What should I do differently?
#include <iostream>
int main()
{
struct Bucket
{
int values[10];
Bucket()
{
n=0;
t=0;
}
};
int* Ordnung[10];
Bucket Neuerbucket;
for (int i=0; i<10; i++)
Neuerbucket.values[i]=i;
for (int i=0; i<10;i++)
std::cout<< Neuerbucket.values[i];
std:: cout<< '\n';
int* array[10];
for (int j=0; j<10; j++)
{
*array[j]=(Neuerbucket.values[j]);
std:: cout<<*array[j];
}
std:: cout << "TROLOL";
return 1;
}
NOTE: This is not a program that should do anything, it is just for the better understanding of pointers and classes/structs. The knowledge I hope to get through your answers will hopefully help me to program my data structure program.
Thanks in advance, you're the best, J.K. :)
This line:
*array[j]=(Neuerbucket.values[j]);
Should probably be:
array[j]=&Neuerbucket.values[j];
Otherwise you're putting int values from Neuerbucket wherever the uninitialized pointers of array point.
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
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?
The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.