Problem with char* as a parameter

N

Neil

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;
}
 
N

Noah Roberts

Neil said:
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.
 
V

Victor Bazarov

Neil said:
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
 
N

Neil

Neil said:
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?
 
N

Noah Roberts

Neil said:
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.
 
J

James Kanze

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.
[/QUOTE]
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.
 
N

Noah Roberts

James said:
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.
 
J

Jim Langston

Neil said:
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.
 
N

Noah Roberts

Jim said:
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.
 
R

Robert Bauck Hamar

Noah said:
Jim said:
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.'
 
J

James Kanze

Jim said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top