By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

returning a char array

P: n/a
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

Feb 19 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Feb 19 '07 #2

P: n/a
On Feb 19, 7:48 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
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

Feb 19 '07 #3

P: n/a
On Feb 19, 6:28 pm, "Bill" <bill.war...@allstate.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*/);

Feb 19 '07 #4

P: n/a
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_size);

// 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.

Feb 19 '07 #5

P: n/a
Bill wrote:
On Feb 19, 7:48 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>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

Feb 19 '07 #6

P: n/a
On Feb 19, 8:30 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
Bill wrote:
On Feb 19, 7:48 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
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_LENGTH 102
#define MAX_FIRSTNAME_LENGTH 51
#define MAX_LASTNAME_LENGTH 51
#define MAX_NUMBER_LENGTH 13

int CheckName(char[MAX_FULLNAME_LENGTH]);

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

Feb 19 '07 #7

P: n/a

"Bill" <bi*********@allstate.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
On Feb 19, 7:48 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>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
Feb 19 '07 #8

P: n/a
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.securityfocus.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
Feb 20 '07 #9

P: n/a
On 19 Feb 2007 07:03:26 -0800, "Bill" <bi*********@allstate.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_LENGTH 102
#define MAX_FIRSTNAME_LENGTH 51
#define MAX_LASTNAME_LENGTH 51
#define MAX_NUMBER_LENGTH 13

int CheckName(char[MAX_FULLNAME_LENGTH]);

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
Mar 4 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.