Hi all,

I tried one program to understand the concept of "Default Arguments" that program works prefectly in Dev C++ but not in Turbo C++ compiler(Ver 3.0).

Here's my program

#include <iostream.h>
#include <stdio.h>
using namespace std;



void justcall(char *c="Parthiban");
//char a[10]; //Global declaration needs for Turbo C++ Compiler 

void main()
{
 char *ch;
 char *accept();
 char flag='y';
 cout<<"Do u want to enter ur name:[y/n] ";
 cin>>flag;
 if(flag=='n')
     justcall();
 else
    justcall(ch=accept());
 fflush(stdin);
 cin.get();
}

void justcall(char *a)
 {
  cout<<"Hello "<<a<<"!\n";
  fflush(stdin);
  cin.get();
 }

 char* accept()
 {
  char a[10]; //Local declaration
  char *p;
   cout<<"Please enter ur name: ";
   cin>>a;
   p=a;
   return p;
 }

As i mentioned in the comment this program works only when character array ( a[10]) globally declared( for Turbo Compiler) .If i use locally defined array it displays garbage value .
I think it's scope vanished but how it is possible ?

Please tell me what is going on behind the scenes ?

and also clarify this doubt .

I need to call cin.get() twice ( main function justcall)function to temporary halt the program to view the results. Is it not enough to place only in main function(since anyhow the control will be return back to main function) .


Thanks in advance .

I can't possibly cover everything here, but to make it short: that code is awful. Here's some links that I have lying around:

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376
http://www.devx.com/tips/Tip/14447

>char *accept();
Yikes.

Since you're using C++, is there any particular reason why you're using C strings instead of the newer C++ ones?

>char a[10]; //Local declaration
You said it yourself: the variable is local. So what happens when the function returns? Well, of course, not only does the variable go out of scope, but the memory is deleted, and the pointer you return is to dead memory. You may want to use the static keyword to keep the memory for the life of the program.

why is accept() not prototyped?

Hi mariocatch, I have prototyped inside main function.

Thanks Joeprogrammer ,I'm just a beginner to C++ i will followthe standards in future .i have learnt a bunch of informations from your reply. Thanks a lot .

Even though the same program works prefectly in Dev C++ compiler without scope problem(i.e in Local scope) .

Please tell me how it is possible ?

> Please tell me how it is possible ?
You're returning a pointer to a local variable.
So when the function returns, the local variable disappears, and the pointer is invalid.

Note that this does NOT GUARANTEE that your code will break. With undefined behaviour, absolutely anything can happen, including producing the expected answer (always a tricky one this, because it makes you think you're right, when in fact you're wrong).

> that program works prefectly in Dev C++ but not in Turbo C++ compiler(Ver 3.0).
Which is why it's best to learn the language to decide what is correct (or not).
Trying to learn C++ by observing the behaviour of a range of compilers is no way to go.

Essentially, you should be able to look at that code, see that a pointer to a local variable is being returned and decide that it is wrong without actually trying it.

This question has already been answered. Start a new discussion instead.