473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

a curious questions about return type

char * get_month(double number){
char month[10];
blah blah

return month
};

int main(){

printf("I want to print %s\n",get_month(whatever);

}
Will this be illegal, since the pointer is pointing to the local
variable month which will be released soon?
What's the nice way of returning a string?

Thanks

Apr 21 '06 #1
9 1472
Yes it will be illegal since month's scope is, just as you said, only
local.

You can read about returning arrays here:
http://www.eskimo.com/~scs/cclass/int/sx5.html

Apr 21 '06 #2
questions? wrote:

char * get_month(double number){
char month[10];
blah blah

return month
};

int main(){

printf("I want to print %s\n",get_month(whatever);

}

Will this be illegal, since the pointer is pointing to the local
variable month which will be released soon?
Not "soon", but "right now".
What's the nice way of returning a string?


One way is to make the buffer static in get_month():

char *get_month(double number) /* Why "double"? */
{
static char month[10]; /* Is this long enough? */
...
return(month);
}

Another method is to pass the buffer to get_month():

char *get_month(double number,char *month)
{
...
return(month);
}

int main()
{
char buffer[10]; /* Is this long enough? */
...
printf("I want to print %s\n",get_month(whatever,buffer));
}

There are other methods, such as malloc() the buffer, but then you have to
make sure to document that the caller needs to free() it.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Apr 21 '06 #3


Murkland wrote On 04/21/06 13:48,:
Yes it will be illegal since month's scope is, just as you said, only
local.


The scope isn't a problem; it's entirely possible
to use objects whose identifiers are not in scope. The
trouble comes from the storage duration: an `auto' object
ceases to exist when execution leaves its containing block.
Thus, any pointer to such an object becomes indeterminate
and unsafe to use.

--
Er*********@sun.com

Apr 21 '06 #4
"questions?" <un************@hotmail.com> writes:
char * get_month(double number){
char month[10];
blah blah

return month
};

int main(){

printf("I want to print %s\n",get_month(whatever);

}
Will this be illegal, since the pointer is pointing to the local
variable month which will be released soon?
What's the nice way of returning a string?


It's not illegal (i.e., the implementation is not obligated to
diagnose it), but any reference to the returned value invokes
undefined behavior.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 21 '06 #5
Murkland wrote:
Yes it will be illegal since month's scope is, just as you said, only
local.


Please review the information below.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 21 '06 #6
questions? wrote:
char * get_month(double number){
char month[10];
blah blah

return month
};

int main(){

printf("I want to print %s\n",get_month(whatever);

}
Will this be illegal, since the pointer is pointing to the local
variable month which will be released soon?
What's the nice way of returning a string?

Thanks


Just replace
char month[10];
with
char *month = malloc(10);

Just be sure to #include <stdlib.h>, and to document that the calling
function needs to handle the deallocation (this is important because any
code could become a library if need be).
Apr 21 '06 #7
Andrew Poelstra wrote:

questions? wrote:
char * get_month(double number){
char month[10];
blah blah

return month
};
[...]
Just replace
char month[10];
with
char *month = malloc(10);

Just be sure to #include <stdlib.h>, and to document that the calling
function needs to handle the deallocation (this is important because any
code could become a library if need be).


And be sure to document that the caller is responsible for free()ing the
returned buffer when done, in order to prevent memory leaks.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Apr 23 '06 #8
Kenneth Brody <ke******@spamcop.net> wrote:
Andrew Poelstra wrote:

questions? wrote:
char * get_month(double number){
char month[10];
blah blah

return month
};

[...]

Just replace
char month[10];
with
char *month = malloc(10);

Just be sure to #include <stdlib.h>, and to document that the calling
function needs to handle the deallocation (this is important because any
code could become a library if need be).


And be sure to document that the caller is responsible for free()ing the
returned buffer when done, in order to prevent memory leaks.


Erm... that's what Andrew just said.

You also need to make sure that your function (and its caller) handles a
null return from malloc() cleanly, though.

Richard
Apr 24 '06 #9
Richard Bos wrote:

Kenneth Brody <ke******@spamcop.net> wrote:
Andrew Poelstra wrote: [...]
Just be sure to #include <stdlib.h>, and to document that the calling
function needs to handle the deallocation (this is important because any
code could become a library if need be).


And be sure to document that the caller is responsible for free()ing the
returned buffer when done, in order to prevent memory leaks.


Erm... that's what Andrew just said.

[...]

Umm... Err... Mmm... Well...

*Thwack*thwack*thwack*

Sorry. I feel much better now.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Apr 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
12
by: Sathyaish | last post by:
Please forgive my nescience. I have worked extensively on Win32 but I am only familiar with C and C++. Lately, I have been practicing C from K&R. Here 're a few doubts I have written in the...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
2
by: Matthew Hood | last post by:
My company has expressed a desire to convert an existing MS Access application to a full VB.NET application. My experience is with VB6 so I want to ask a few questions and get some input on the...
1
by: E.T. Grey | last post by:
Hi All, Despite spending the past six to seven hours perusing the docs on the mySQl site, I still have questions unanswered, and have been unable to get any help. I am familiar with Sybase, some...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
8
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.