What, if anything, is wrong with the following code fragment?

C

Chris

Could anyone please help me. IS there something wrong with it?


char * function getString()

{

char myString[20];

sprintf(myString, "hello world");

return myString;

}
 
K

Karsten Baumgarten

Chris said:
Could anyone please help me. IS there something wrong with it?


char * function getString()

{

char myString[20];

sprintf(myString, "hello world");

return myString;

}

Your function declaration is wrong. It must be

char *getString()

The function returns an adress of a local variable and your compiler
should have warned you already. The problem is, by the time you return
the adress it is already invalid. That is, the content of myString no
longer exists, yielding into some strange output if you try to print the
result of the function (arbitrary characters, if you are lucky they
might even make some sense ;) ).
 
T

Tim Love

Chris said:
Could anyone please help me. IS there something wrong with it? Yes.

char * function getString()
{
char myString[20];
sprintf(myString, "hello world");
return myString;
}

myString is created within the routine but the 20 bytes of space it
uses is freed (because it's a local variable) when the function ends
and can be re-used for other reasons. The function returns a pointer
to the start of this space. You might initially find that "hello world"
is still there, but that's just luck.

http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/argsC++.html
is overkill, but might be of use.
 
R

Richard Herring

Chris said:
Could anyone please help me. IS there something wrong with it?


char * function getString()

{

char myString[20];

sprintf(myString, "hello world");

return myString;

}
You're returning the address of a local variable. By the time
getString() returns, myString no longer exists.
 
R

Ron Natalie

Chris said:
Could anyone please help me. IS there something wrong with it?

Yes, it involves the use of arrays and pointers by someone who doesn't
understand them. You should avoid them

std::string getString() {
return "hello world";
}
 
R

Ron Natalie

BigBrian said:
or,

char * getString()
{
return "hello world";
}

so you don't have to create a std::string object if all the caller
wanted is a char *.
Which would be dangerous since the string literal is really const.
Return const char* or make a proper copy.

However, my point still stands. Beginning programmers should get
it out of their heads that char* is some sort of string type.
 
B

BigBrian

Could anyone please help me. IS there something wrong with it?
Yes, it involves the use of arrays and pointers by someone who doesn't
understand them. You should avoid them

std::string getString() {
return "hello world";
}

or,

char * getString()
{
return "hello world";
}

so you don't have to create a std::string object if all the caller
wanted is a char *.
 
B

BigBrian

Could anyone please help me. IS there something wrong with it?
Yes, it involves the use of arrays and pointers by someone who doesn't
understand them. You should avoid them

std::string getString() {
return "hello world";
}

or,

char * getString()
{
return "hello world";
}

so you don't have to create a std::string object if all the caller
wanted is a char *.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top