May I know use of Method returning a pointer in C++, please?

Thanks.

Recommended Answers

All 7 Replies

May I know use of Method returning a pointer in C++, please?

What you are saying resembles english and seems quite insightful, so I will offer you this:

#include<iostream>

int* get_address(int&);

int main()
{
     int number = 0;

     std::cout << "Enter a number: ";
     std::cin >> number;

     std::cout << "The memory address for the number is: " << get_address(number);

return 0;
}

get_address(int& num_addr)
{
     return &num_addr;
}

Minor fix :

#include<iostream>

int* get_address(int&);

int main()
{
int number = 0;

std::cout << "Enter a number: ";
std::cin >> number;

std::cout << "The memory address for the number is: " << get_address(number);

return 0;
}

int *get_address(int& num_addr)
{
return &num_addr;
}

good catch..

i need to get a compiler on me' old laptop.

They have spell check on here.. why don't they have code check ;)

Thank You.

What will return if the static cast fails for pointers with polymorphic object?
What will return if the static cast fails for reference with polymorphic object?
What will return if the static cast fails for pointers with non-polymorphic object?
What will return if the static cast fails for reference with non-polymorphic object?
Can you do static casting with structures in the polymorphic object?
Can you do static casting with structures in the non-polymorphic object?
Can you do static casting with unions in the polymorphic object?
Can you do static casting with unions in the non polymorphic object?

Thanks.

No one is going to do your homework coder2009, but nice try.

If you have a problem with these questions.. think further and ask more specific what exactly you don't understand. For example in the first question.. do you know what a static cast is? etc.

If you show that you tried yourself first then people will be happy to help.

Here is just simple rumbling around same example

#include<iostream>

int sum = 0;

int main()
{
     int x, y;

     std::cout << "Enter number X: ";
     std::cin >> x;
     
     std::cout << "Enter number Y: ";
     std::cin >> y;

     std::cout << "The memory address for the number is: " << *Summation(number);

return 0;
}

int* Summation(int x,  int y)
{
     sum = x+y;
     return& sum;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.