?
Solved

Passing default argument as a pointer

Posted on 2003-03-28
3
Medium Priority
?
287 Views
Last Modified: 2010-04-01
fun(int *p)
{
............
}

int main()
{
int i;
fun(&i);
}

In the above function I want to initialize the value of i as 99, with the concept of default argument in C++. ie. the content of p or *p will be 99 in the function fun().Here I am passing argument as a pointer to the int variable so how to do that?
0
Comment
Question by:Francoz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:skyDaemon
ID: 8227257
When you use a default argument you do not pass anything in.  Also, you cannot default the dereferenced result of a pointer, so you'll have to change int* p to just int p.  If you need a pointer for a return value you'll have to add a second parameter for that (one for the default initial value, one for the pointer for the return value).

In the header declare fun as:

fun(int p = 99);

Then in main,

int main()
{
fun();
}

This will use 99 in the fun function.

0
 
LVL 1

Accepted Solution

by:
ivec earned 150 total points
ID: 8227333
If I understand what you want correctly, you cannot use default-argument syntax for this.
But you could use function overloading instead:

void f(int* p) { /* do stuff */ }
void f()
{
  int i = 99;
  f(&i);
}

Or:

void f(int* p=0) // pass NULL pointer by default;
{
  int default_int = 99;
  if( p == 0 ) p = &default_int;

  /* do stuff */
}

I hope this helps,
0
 
LVL 2

Author Comment

by:Francoz
ID: 8236174
The second answer by ivec seems to be a better choice. But it is a logical method, where I'm seeking one standard C++ method. So I'll explain my qn further.

I'll be passing one pointer to an integer into the function f() as follows.

void f(int *p)
{
//doing something
}

But while calling f() I need the content of p to be initialised with 99, if nothing is passed specifically.

Please note down the point if nothing is passed.

Why I mentioned default argumets, because say if it was a normal int variable then I can have declaration like this.

void f(int i = 99)
{
//do something.
}

More than that I wish to do this without an extra cost of function overloading etc.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question