By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Problem with char* as a parameter

P: n/a
I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.

Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.

#include <iostream>
using namespace std;

void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}

int main ()
{
char* info[9];
test (info);
cout << info[0] << endl;
}

Jun 11 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Neil wrote:
I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.

Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.

#include <iostream>
using namespace std;

void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}
Change the first line to:

static char name[] = "My Name";

I'm going to let you figure out why that is necessary, great thought
exercise that will teach you a rather fundamental thing.
Jun 11 '07 #2

P: n/a
Neil wrote:
I am working on a code that is supposed to return a string to my main
function from the test function.
std::string my_test_function();
The cout in test returns "My Name",
but the cout in main returns some junk characters.

Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.
There is no fixing applicable to your program. It's beyond fixing.
You need to rewrite it in terms of 'std::string'.
>
#include <iostream>
using namespace std;

void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}

int main ()
{
char* info[9];
test (info);
cout << info[0] << endl;
}
#include <iostream>
#include <string>
using namespace std;

std::string test()
{
return "My Name";
}

int main()
{
cout << test() << endl;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 11 '07 #3

P: n/a
On Jun 11, 11:12 am, Noah Roberts <u...@example.netwrote:
Neil wrote:
I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.
Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.
#include <iostream>
using namespace std;
void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}

Change the first line to:

static char name[] = "My Name";

I'm going to let you figure out why that is necessary, great thought
exercise that will teach you a rather fundamental thing.
Thanks, it's working. Was the problem that name was declared inside
the test function and temp[0] was only storing the pointer to the
name, which would be recycled once the function returned, and making
it a class variable solves the problem of the value in name getting
lost?

Jun 11 '07 #4

P: n/a
Neil wrote:
Thanks, it's working. Was the problem that name was declared inside
the test function and temp[0] was only storing the pointer to the
name, which would be recycled once the function returned, and making
it a class variable solves the problem of the value in name getting
lost?
Interesting theory. You should test it.
Jun 11 '07 #5

P: n/a
On Jun 11, 7:16 pm, Neil <nill...@gmail.comwrote:
On Jun 11, 11:12 am, Noah Roberts <u...@example.netwrote:
Neil wrote:
I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.
Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.
#include <iostream>
using namespace std;
void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}
Change the first line to:
static char name[] = "My Name";
I'm going to let you figure out why that is necessary, great thought
exercise that will teach you a rather fundamental thing.
Thanks, it's working. Was the problem that name was declared inside
the test function and temp[0] was only storing the pointer to the
name, which would be recycled once the function returned, and making
it a class variable solves the problem of the value in name getting
lost?
No. In fact, there aren't any pointers in this code.

Doesn't the text you are using to learn C++ speak about the
lifetime of objects? It's a very important point, and should be
treated fairly early.

Also, doesn't your text talk about std::string and std::vector?
I'ts probably what you should be using here; the old, C style
arrays are seriously broken, and very difficult to use
correctly.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 11 '07 #6

P: n/a
James Kanze wrote:
On Jun 11, 7:16 pm, Neil <nill...@gmail.comwrote:
>On Jun 11, 11:12 am, Noah Roberts <u...@example.netwrote:
>>Neil wrote:
I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.
>>>Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.
>>>#include <iostream>
using namespace std;
>>>void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}
No. In fact, there aren't any pointers in this code.
Sorry, you are mistaken. There's a whole array of them plus the
temporary one created by the use of "name" as an rvalue.
Jun 11 '07 #7

P: n/a

"Neil" <ni*****@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>I am working on a code that is supposed to return a string to my main
function from the test function. The cout in test returns "My Name",
but the cout in main returns some junk characters.

Can anyone tell me how I could fix it so I get "My Name" printed from
main also. Thanks.
#include <iostream>
using namespace std;

void test (char * temp[])
{
char name[] = "My Name";
temp[0] = name;
cout << temp[0] << endl;
}

int main ()
{
char* info[9];
test (info);
cout << info[0] << endl;
}
Making char name[] to static char name[] solves your immediate problem as
then name doesn't go out of scope.

See if you can determine why the following does the same thing:

#include <iostream>

void test (char **temp)
{
char* name = "My Name";
*temp = name;
std::cout << temp[0] << std::endl;
}

int main ()
{
char* info;
test (&info);
std::cout << info << std::endl;
}

To help you out, you declared info as an array of 9 pointers to character.
That is, you could of had 9 different names it was pointing to. To store
the address of where "My Name" is only requires one pointer.

Of course there is the more C++ way using functions:

#include <iostream>
#include <string>

void test (std::string& temp)
{
char* name = "My Name";
temp = name;
std::cout << temp << std::endl;
}

int main ()
{
std::string info;
test (info);
std::cout << info << std::endl;
}

Note: In the first version I could of declared the function as char *& (a
reference to a character pointer) but since I think you're trying to
understand points I left it as a pointer.
Jun 12 '07 #8

P: n/a
Jim Langston wrote:
Making char name[] to static char name[] solves your immediate problem as
then name doesn't go out of scope.

See if you can determine why the following does the same thing:

#include <iostream>

void test (char **temp)
{
char* name = "My Name";
*temp = name;
std::cout << temp[0] << std::endl;
}

int main ()
{
char* info;
test (&info);
std::cout << info << std::endl;
}
You are in the realm of undefined behavior. This program may result in
what you "want". In fact under some implementations I would bet it
does. The reason is that in this case, "My Name", is often placed in a
static and global location. Therefor any pointer you assign the address
to will have access to that location...it isn't like the last version
where "My Name" is being placed in a memory segment allocated on the
stack for that function.

Either one I think could technically result in anything but this example
is more likely to show major discrepancies between versions and/or act
like it works just fine. Case in point, it prints, "My Name," in the
output call from main on my implementation.
Jun 12 '07 #9

P: n/a
Noah Roberts wrote:
Jim Langston wrote:
>Making char name[] to static char name[] solves your immediate problem as
then name doesn't go out of scope.

See if you can determine why the following does the same thing:

#include <iostream>

void test (char **temp)
{
char* name = "My Name";
*temp = name;
std::cout << temp[0] << std::endl;
}

int main ()
{
char* info;
test (&info);
std::cout << info << std::endl;
}

You are in the realm of undefined behavior.
No.
This program may result in what you "want".
Yes.
In fact under some implementations I would bet it does.
Under all conforming implementations, it does.
The reason is that in this case, "My Name", is often placed in a
static and global location.
Yes. It is mandated by the standard. §2.13.4: 'An ordinary string literal
has type "array of n const char" and static storage duration.'

--
Robert Bauck Hamar
Jun 12 '07 #10

P: n/a
On Jun 12, 5:57 pm, Noah Roberts <u...@example.netwrote:
Jim Langston wrote:
Making char name[] to static char name[] solves your immediate problem as
then name doesn't go out of scope.
The name "name" certainly does go out of scope. What you mean
is that the lifetime of the object doesn't end.
See if you can determine why the following does the same thing:
#include <iostream>
void test (char **temp)
{
char* name = "My Name";
*temp = name;
std::cout << temp[0] << std::endl;
}
int main ()
{
char* info;
test (&info);
std::cout << info << std::endl;
}
You are in the realm of undefined behavior.
Where? I don't see any undefined behavior there.
This program may result in what you "want". In fact under
some implementations I would bet it does. The reason is that
in this case, "My Name", is often placed in a static and
global location.
The expression "My Name" is a string literal. String literals
have static lifetime, which means that they exist for the
lifetime of the program. The address of a string literal is
always valid.

Functions which return the address of a string literal are
legion, and have been since the earliest days of C, so any
compiler which didn't get this right would have been out of
business ages ago.
Therefor any pointer you assign the address
to will have access to that location...it isn't like the last version
where "My Name" is being placed in a memory segment allocated on the
stack for that function.
Either one I think could technically result in anything but this example
is more likely to show major discrepancies between versions and/or act
like it works just fine. Case in point, it prints, "My Name," in the
output call from main on my implementation.
And is guaranteed to do so on any conforming implementation.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 13 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.