in one file I define

void Show(char* s){
     string str =" ";
     str+=s;
     cout<<str<<endl;
}

then in main I call it by

char s[100]="Testing...";
      //or use *s here, make no difference
      Show(s);

output nothing but blank. weird?

I actually simplify the code to put here, the str indeed contains something before I
add s into it. So in the scenario u mentioned, the code will only output a "g", follow
by blank space.

btw I use g++ under Redhat.

What happens if you make str = " g" or something similar?

sorry I kind of simplify it too much, the Show(char *s) method is actually part of a
util class

class util{
   public:
   ...
   void Show(char* s);
}

in main I use

util u;
     u.Show("something");   // method 1

     util *u=new util();
      u->Show("something");   //method 2

first method cause a segmentation fault.
second one compile and running ok, but output blank space.
i am always out of my mind in small place


I actually simplify the code to put here, the str indeed contains something before I
add s into it. So in the scenario u mentioned, the code will only output a "g", follow
by blank space.

btw I use g++ under Redhat.

First, I would suggest changing the function to be static, so that you can call it like this:

util::Show( "something");

Thanks for suggestion, the problem is I have to initialize some variables in the
constructor of util so I am not sure if util::Show will work.

This article has been dead for over six months. Start a new discussion instead.