473,498 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with char* as a parameter

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
10 1733
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
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
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
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
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2373
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do...
6
4900
by: Sagar Choudhary | last post by:
I m using a library function which need to be passed a function as parameter. If i declare the function as int (void *, int, char **, char **) it throws an error cannot convert parameter from...
1
1219
by: Sh0t | last post by:
Everytime i compile this program the pointer seems to lose its data?? *fileToFix is suppose to change after entering fixFile() as a param program ------------------------------------------------...
3
5050
by: Timo | last post by:
I am trying to pass a pointer to an array of structures to function, but I have some problems. I am using MS Visual C++ 6.0, but I think this is more of a C-problem than Windows programming...
33
2213
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
8
10678
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
0
3907
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
3
1752
by: Bilbo | last post by:
I have a a headscratcher here: I have a form that when submitted should do 2 things when a user enters data and then clicks the Add button. Here goes: 1. Call a stored procedure called...
3
2619
by: iskeletor | last post by:
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #define STUDENT_NUMBER 68 #define ARRAY_LENGTH 10 struct node{ char Name,Surname; int data,no;
2
1738
by: Jeff | last post by:
hi asp.net 2.0 I have a gridview which I'm trying to update a row within. When I click on the Update button, my website crash. It displays a webpage saying that a parameter is missing. So I...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.