a function returning a char array

A

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?
 
P

Peter Nilsson

Angus said:
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.
 
E

Eric Sosman

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

Michael

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.

> 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?
>
>
 
M

Mark McIntyre

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.

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
 
M

Michael

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... ;-)
 
C

CBFalconer

Angus said:
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)".
 
R

Richard Heathfield

Michael said:
Yes. This is true.


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

Jamie Boy

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

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top