468,768 Members | 1,509 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,768 developers. It's quick & easy.

a function returning a char array

Hello

I want to write a function which returns a char array.

But if I write this:

char[] myfunction()
{
return "123";
}

I get a C4091 warning and a C2143 error on compiling. so how do I write a
function which returns a char array?
Jan 3 '07 #1
9 2751
Angus wrote:
Hello

I want to write a function which returns a char array.
You can't in standard C. No function can receive or return an array.
[1]

[Function parameters can be declared as arrays, but they are
interepreted as a pointer to the type of the array element.]
But if I write this:

char[] myfunction()
{
return "123";
}

I get a C4091 warning and a C2143 error on compiling. so how do I write a
function which returns a char array?
You can't directly. What is normally done in C is to declare your
function
as taking a pointer to a destination, as in...

void myfunction(char *dest)
{
strcpy(dst, "123");
}

Thus the caller of myfunction needs to allocate the relevant space and
the
function populates that space.
[1] You can receive and return a struct or union that contains an array
as one of its elements, but the larger the object, the worse an idea
that becomes.

--
Peter

Jan 3 '07 #2
Angus wrote On 01/03/07 17:36,:
Hello

I want to write a function which returns a char array.

But if I write this:

char[] myfunction()
{
return "123";
}

I get a C4091 warning and a C2143 error on compiling. so how do I write a
function which returns a char array?
By switching from C to some other language.

C functions cannot return arrays. Even if they could,
arrays are not assignable: You can't write

target_array = array_function();

Instead, a C function can return a pointer to one of
the elements of an array (often the first, the one at
index [0]), and the caller can store that returned value
in a pointer variable. So

char *myfunction(void) {
return "123";
}
...
char *string = myfunction();

is all right. There are, however, some pitfalls; the good
news is that if you fall into them you'll have a fairly
soft landing on the decomposing bodies of the myriad of
programmers who have fallen before you ...

Several careful readings of the FAQ (http://www.c-faq.com)
are highly recommended, and keep your C textbook at your elbow.

--
Er*********@sun.com
Jan 3 '07 #3

Isnt it possible to have the array GLOBAL and then point to that array
as a return value? (I cant remember.)

I only know this (that C/C++ is very poor in usage of pointer and the
like. Realy.)

Maybe, this is helpful:

Read always from the right-to-the-left. But as a C/C++ programmer, you
should bekome R-T-L (right-to-left) reader/writer. Like the hebrew language.

1. *Declaration*

int *zeig_1; // Read: zeig_1 is a pointer to int.
// There is nothing "in it", its just a declaration here.

2. *Do something*. *Point to someting*.

zeig_1 = &var_1; // 'zeig_1' points to the variable 'var_1'
// in zeig_1 is now the memory-address stored
// of where var_1 is.

*Example*:

int var_1; // an int
int var_2; // another int
int *zeig_1; // a pointer to int (read from right-to-left)

int var_1 = 4; // 'var_1' = 4

zeig_1 = &var_1; // setup pointer to point to the int variable.
// read: var_1 is an address. zeig_1 IS/contains now
// this address. And AT this address, there is an int
// value of 4 stored. --- You must think in
// assemble/machine code...

var_2 = *zeig_1; // Now. var_2 is the value where zeig_1
// points to. :-)

*zeig_1 = 7; // change the value, where zeig_1 points to, to 7.
// So, this was nice. :-)
// ( *zeig_1 is var_1 )
In C/C++, you must think like in assembler/machine-code... and how the
things are layouted there pysically... ;-)

*What happend in the memory is this*:

-----------
<< zeig_1 >>
-----------
||
\/
----------- -----------
< var_1 < var_2 >
----------- -----------
| |
v v
----------- -----------
| 4 : 7 | | 4 |
----------- -----------
*You can also have things like that*:

int x; // x is an int
int *pi; // pi is a pointer to int

int * const cpi = &x; // cpi is a constant pointer to int.
// and will be initialized, that it
// points to x (this is the address of x).
// :-)
*NOTE*:
Read here:
1. the left side of the "="-sign from right to left.
2. then read the right side of the "="-sign, also from the
right-to-the-left. ;-)

C/C++ brainwashed my mind.
Angus wrote:
Hello

I want to write a function which returns a char array.

But if I write this:

char[] myfunction()
{
return "123";
}

I get a C4091 warning and a C2143 error on compiling. so how do I
write a
function which returns a char array?


Jan 3 '07 #4
On Thu, 04 Jan 2007 00:22:43 +0100, in comp.lang.c , Michael
<mi*****@mustun.chwrote:
>
Isnt it possible to have the array GLOBAL and then point to that array
as a return value? (I cant remember.)
Sure, but its very bad style, and will give major problems if you are
writing a library, or working in a re-entrant environment.
>I only know this (that C/C++ is very poor in usage of pointer and the
like. Realy.)
I disagree.
I get a C4091 warning and a C2143 error on compiling. so how do I write a
function which returns a char array?
You can't, in C. You can return a pointer to malloc'ed memory, or you
can fill out an array you passed in, however.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jan 3 '07 #5

And... you can write things like that:

const int *pci; // pci is a pointer to a const int (int const).
// read from right-to-left ("int const", but
// "const int" sounds better and is more
// logical... :-)

*pci = 10; // pci points to a const value. (not allowed)
pci++; // pci points to the next const int. (OK)
const cx;
const int * const cpci = &cx;

// cpci is a const pointer to a const int. more constand you can't have
it... ;-)

Jan 3 '07 #6
Sure, but its very bad style, and will give major problems if you are
writing a library, or working in a re-entrant environment.
Yes. This is true.
>
>>I only know this (that C/C++ is very poor in usage of pointer and the
like. Realy.)


I disagree.
Why?

Jan 3 '07 #7
Angus wrote:
>
I want to write a function which returns a char array.

But if I write this:

char[] myfunction()
{
return "123";
}

I get a C4091 warning and a C2143 error on compiling. so how do
I write a function which returns a char array?
You don't. You write a function that returns a pointer to a char
array, which may be a string. In the above, use "const char
*myfunction(void)".

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 4 '07 #8
Michael said:
>
>Sure, but its very bad style, and will give major problems if you are
writing a library, or working in a re-entrant environment.

Yes. This is true.
>>
>>>I only know this (that C/C++ is very poor in usage of pointer and the
like. Realy.)


I disagree.

Why?
I can't speak for Mark (and why did you not attribute his writing to him?),
but I agree with him, and my reasoning is this: I find C's pointer
mechanisms simple, logical, and powerful. They have been used to tremendous
effect by all kinds of people in all kinds of systems and applications.
Either you understand them (in which case I suspect you'd agree that they
work just fine) or you don't (in which case you are in no position to judge
whether or not they are poor). If you truly understood them and still
thought them poor, you'd have a case that was at least worth hearing, but
your reply earlier in this thread suggests that you don't truly understand
them.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 4 '07 #9
Angus:
Hello

I want to write a function which returns a char array.

But if I write this:

char[] myfunction()
{
return "123";
}

I get a C4091 warning and a C2143 error on compiling. so how do I write
a
function which returns a char array?

Is the length of the array known at compile time? If so:

typedef struct ArrayFourChar {
char arr[4];
} ArrayFourChar;

ArrayFourChar Func(void)
{
ArrayFourChar const afc = {"123"};
return afc;
}

If not, you can just make it big enough every time:

typedef struct Array256Char { ...

Failing that, you've to use dynamic allocation. Then you'll have to decide
which part of the program is responsible for deallocating the memory. A
sample could be something like:

#include <stdlib.h>

char *const Func(unsigned const i)
{
char *const p = malloc(i);
char *register q = p;

if (!p) exit(EXIT_FAILURE);

*q++ = 'a';
*q++ = 'b';
*q++ = 'c';
*q = 0;

return p;
}

int main(void)
{
char *const p = Func();

free(p);
}

--
~/JB\~
Feb 7 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Tweaxor | last post: by
5 posts views Thread by shyam | last post: by
6 posts views Thread by M Turo | last post: by
4 posts views Thread by prashant.khade1623 | last post: by
5 posts views Thread by Fei Liu | last post: by
26 posts views Thread by aruna.mysore | last post: by
13 posts views Thread by Sri Harsha Dandibhotla | last post: by
10 posts views Thread by Richard Heathfield | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.