Hi experts,
I'm trying to return a char[] from a function. How would this be
setup and what would the declaration look like?
Thanks,
Bill 9 8248
Bill wrote:
Hi experts,
I'm trying to return a char[] from a function. How would this be
setup and what would the declaration look like?
It would be set up in a language other than C, and would
look like that other language. C functions cannot return
arrays, period.
Take one step back, retreating from the solution you have
imagined to the problem you are facing: What are you trying
to do?
--
Eric Sosman es*****@acm-dot-org.invalid
On Feb 19, 7:48 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
Bill wrote:
Hi experts,
I'm trying to return a char[] from a function. How would this be
setup and what would the declaration look like?
It would be set up in a language other than C, and would
look like that other language. C functions cannot return
arrays, period.
Take one step back, retreating from the solution you have
imagined to the problem you are facing: What are you trying
to do?
--
Eric Sosman
esos...@acm-dot-org.invalid
I have a function that opens a file that I am trying to open that will
contain menu items (strings) that
will be displayed to a user. I need to return the data from the file.
Bill
On Feb 19, 6:28 pm, "Bill" <bill.war...@al lstate.comwrote :
Hi experts,
I'm trying to return a char[] from a function. How would this be
setup and what would the declaration look like?
Thanks,
Bill
You will need to return a static char array or a dynamically allocated
one.
For a static array you'll need to be careful to copy the return value
(to another array in the calling function) if you're going to call the
function more than once. For dynamically allocated memory you'll need
to remember to free the memory once you've finished using it.
The declaration will look like:
char *foo(/*formal parameters go here*/);
char *fn1(parameter list)
{
static char array[x];
// here somehow ensure that array[] has a terminating '\0'
return array;
}
or
char *fn2(parameter list)
{
char *str = malloc(some_siz e);
// here also ensure that str has a terminating '\0'
return str;
}
In the calling point, you can access the "char* " returned from the
functions till the terminating '\0' character. If
fn2() is used, the memory returned by it should be freed at the
calling point.
Bill wrote:
On Feb 19, 7:48 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
>Bill wrote:
>>Hi experts, I'm trying to return a char[] from a function. How would this be setup and what would the declaration look like?
It would be set up in a language other than C, and would look like that other language. C functions cannot return arrays, period.
Take one step back, retreating from the solution you have imagined to the problem you are facing: What are you trying to do?
-- Eric Sosman esos...@acm-dot-org.invalid
I have a function that opens a file that I am trying to open that will
contain menu items (strings) that
will be displayed to a user. I need to return the data from the file.
You cannot return an array, but you can return a pointer
to an element of an array (usually the first element, the one
with index [0]). Make sure the array still exists after the
function returns, though; see Questions 7.5a and 7.5b in the
comp.lang.c Frequently Asked Questions (FAQ) list at http://c-faq.com/
--
Eric Sosman es*****@acm-dot-org.invalid
On Feb 19, 8:30 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
Bill wrote:
On Feb 19, 7:48 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
Bill wrote: Hi experts, I'm trying to return a char[] from a function. How would this be setup and what would the declaration look like?
It would be set up in a language other than C, and would
look like that other language. C functions cannot return
arrays, period.
Take one step back, retreating from the solution you have
imagined to the problem you are facing: What are you trying
to do?
--
Eric Sosman
esos...@acm-dot-org.invalid
I have a function that opens a file that I am trying to open that will
contain menu items (strings) that
will be displayed to a user. I need to return the data from the file.
You cannot return an array, but you can return a pointer
to an element of an array (usually the first element, the one
with index [0]). Make sure the array still exists after the
function returns, though; see Questions 7.5a and 7.5b in the
comp.lang.c Frequently Asked Questions (FAQ) list at
http://c-faq.com/
--
Eric Sosman
esos...@acm-dot-org.invalid- Hide quoted text -
- Show quoted text -
I read the FAq and now I'm even more confused. I'll admit it - I'm
lost.
I'm including the code I have so far, but it produces numerous errors:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_FULLNAME_LE NGTH 102
#define MAX_FIRSTNAME_L ENGTH 51
#define MAX_LASTNAME_LE NGTH 51
#define MAX_NUMBER_LENG TH 13
int CheckName(char[MAX_FULLNAME_LE NGTH]);
char *GetMenu();
char Menu[];
int main(void)
{
printf("Please Make a Selection:\n\n" );
//get the menu from the menu file
Menu[] = GetMenu();
return 0;
}
char *GetMenu()
{
FILE *fp;
static char menuArray[15];
fp = fopen("menu.mnu ", "r"); /* open menu.mnu for reading
*/
int i = 0;
for(i = 0;i < 15; i++)
{
if(fgets(str,15 ,fp) != NULL)
{
menuArray[i] = str;
}
}
fclose(fp); /* close the file */
return menuArray;
}
"Bill" <bi*********@al lstate.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
On Feb 19, 7:48 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
>Bill wrote:
Hi experts,
I'm trying to return a char[] from a function. How would this be
setup and what would the declaration look like?
It would be set up in a language other than C, and would look like that other language. C functions cannot return arrays, period.
Take one step back, retreating from the solution you have imagined to the problem you are facing: What are you trying to do?
-- Eric Sosman esos...@acm-dot-org.invalid
I have a function that opens a file that I am trying to open that will
contain menu items (strings) that
will be displayed to a user. I need to return the data from the file.
In that case, your original question is incorrect. You need an array
of char arrays.
--
Fred L. Kleinschmidt
Bill wrote:
>
.... snip ...
- Show quoted text -
What is this silly "show quoted text" doing in your replies? Eric
Sosman did not write anything of the sort.
--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
On 19 Feb 2007 07:03:26 -0800, "Bill" <bi*********@al lstate.com>
wrote:
>I read the FAq and now I'm even more confused. I'll admit it - I'm lost. I'm including the code I have so far, but it produces numerous errors:
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h>
#define MAX_FULLNAME_LE NGTH 102 #define MAX_FIRSTNAME_L ENGTH 51 #define MAX_LASTNAME_LE NGTH 51 #define MAX_NUMBER_LENG TH 13
int CheckName(char[MAX_FULLNAME_LE NGTH]);
char *GetMenu();
char Menu[];
Where in the faq did you see a suggestion that you could define an
array at file scope without providing its size?
> int main(void) { printf("Please Make a Selection:\n\n" );
//get the menu from the menu file Menu[] = GetMenu();
Where in the faq did you see a suggestion that you could assign a
value to an array element without specifying which element?
> return 0;
}
char *GetMenu() { FILE *fp;
static char menuArray[15];
fp = fopen("menu.mnu ", "r"); /* open menu.mnu for reading */ int i = 0;
C89, the version used by most of us, does not permint declarations to
follow statements.
> for(i = 0;i < 15; i++) { if(fgets(str,15 ,fp) != NULL)
Where did you define str? Did you mean to use menuArray here?
> { menuArray[i] = str;
What are you trying to accomplish here?
> } }
fclose(fp); /* close the file */
Do you only have one record to read from the file?
> return menuArray; }
Remove del for email This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Justin Naidl |
last post by:
class Foo
{
protected:
char foo_stuff;
public:
char* get_foo_stuff();
}
Given the above example. What I want to know is the "proper/standard" way
|
by: john |
last post by:
Relatively new to C coding, so any help would greatly be appreciated.
I'm having problems try to return my string array from my parsing
function. When I do a printf I am getting the correct value for my first
element but my subsequent printf's will return garbage. Can someone let
me know what I am doing wrong. Thanks in advance.
-jlewis
|
by: Pete |
last post by:
Can someone please help, I'm trying to pass an array to a function, do some
operation on that array, then return it for further use. The errors I am
getting for the following code are, differences in levels of indirection, so
I feel it must have something to do with the way I am representing the array
in the call and the return.
Below I have commented the problem parts.
Thanks in advance for any help offered.
Pete
|
by: jt |
last post by:
I'm getting a compiler error: warning C4172: returning address of local
variable or temporary
Here is the function that I have giving this error: I'm returning a
temporary char string and its not liking it.
How can I fix this?
char *dequeue(struct node **first)
{
char temp;
|
by: Carramba |
last post by:
hi!
the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question
Iam trying to make program were I enter string and serach char.
and funktion prints out witch position char is found this
is done if funktion serach_char. so far all good
what I want do next is:
return, from funktion, pointer value to array were
positions ( of found char) is stored. and print that array from main.
but I only manage to print memory adress to...
| |
by: shyam |
last post by:
Hi All
I have to write a function which basically takes in a string and
returns an unknown number( at compile time) of strings
i hav the following syntax in mind
char *tokenize(char *)
is it ok?
|
by: Robert Smith |
last post by:
I am wondering why it is possible to return a pointer to a string literal
(ie. 1) but not an array that has been explicitly allocated. (ie. 2) ?
Both would be allocated on the stack, why does the first one not cause a
compiler warning?
#include <stdio.h>
char * funca() {
char *a = "blah"; //1 - ok
// char a = "blah"; //2 - not ok
|
by: kleary00 |
last post by:
Hi,
I am writing a function that needs to return an array of strings and I
am having some trouble getting it right. I need some help.
Here is what I consider an array of 100 strings:
char *array_string
Thanks,
-Kim
|
by: Beorne |
last post by:
I have to call a c++ library funtion returning a string with the
following signature:
char *get_identifier();
Usually when I have to marshal a function with a char* output
parameter I do:
static extern int get_identifier2(
StringBuilder Ack_Msg, int msg_len);
|
by: darren |
last post by:
Hi everybody, have a quick look at this code:
=====
=====
int main(void) {
string msg;
makeString(msg);
cout << "back in main, result = " << msg << endl;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |