return a char array from a function

F

Fernando

Hello,
I want call, from main program, to a function (with void arguments), what it
run the function program and return the data of type char. I have make a
example of code, by it don´t run, I can´t compile. I beleave what I have
problems with the concept of the arrays and the pointers:
#include <stdio.h>
char composefile(void);
main ()
{
char uno[6];
uno = composefile();
printf("%s", uno);
}

char composefile(void)
{
char KK[]="hola";
return (&KK);
}

Someone can say me how to do this.

Thanks,
Fernando
 
A

Allan Bruce

Fernando said:
Hello,
I want call, from main program, to a function (with void arguments), what it
run the function program and return the data of type char. I have make a
example of code, by it don´t run, I can´t compile. I beleave what I have
problems with the concept of the arrays and the pointers:
#include <stdio.h>
char composefile(void);
main ()

you should specify that main returns an int - always
{
char uno[6];

This shoulg be:
char *uno;
uno = composefile();
printf("%s", uno);
}

char composefile(void)

Should return char* i.e.
char *composefile();
{
char KK[]="hola";

either create a static (not recomended) or use a pointer and malloc the
memory, i.e.
char *KK = malloc(5); // or enough space for what you need + 1 for
terminator (not arnie!)
strcpy(KK, "hola")
return (&KK);

If using a pointer, then just:
return KK;
 
A

Allan Bruce

return (&KK);
If using a pointer, then just:
return KK;

Or if using static, it is the same as &KK is a syntax error. You could use
&KK[0] but that is confusing.
Allan
 
N

Nils Petter Vaskinn

char composefile(void)
{
char KK[]="hola";
return (&KK);
}

Your array KK cease to exist once composefile() returns, so the pointer
you return isn't valid.

#define BUFFER_LENGTH 5
#include <stdio.h>
#include <string.h>

void composefile(char *);

int main(void) {
char buffer[BUFFER_LENGTH];
composefile(buffer);
printf("%s",buffer);
}

void composefile(char *b) {
char KK[] = "hola";
strncpy(b,KK,BUFFER_LENGTH);
}
 
P

pete

Fernando said:
Hello,
I want call, from main program, to a function (with void arguments), what it
run the function program and return the data of type char. I have make a
example of code, by it don´t run, I can´t compile. I beleave what I have
problems with the concept of the arrays and the pointers:
#include <stdio.h>
char composefile(void);
main ()
{
char uno[6];
uno = composefile();
printf("%s", uno);
}

char composefile(void)
{
char KK[]="hola";
return (&KK);
}

Someone can say me how to do this.


#include <stdio.h>
#include <string.h>

char *composefile(void);

int main (void)
{
char uno[6];

strcpy(uno, composefile());
printf("%s\n", uno);
return 0;
}

char *composefile(void)
{
char *KK = "hola";

return KK;
}
 
J

j

Fernando said:
Hello,
I want call, from main program, to a function (with void arguments), what it
run the function program and return the data of type char. I have make a
example of code, by it don´t run, I can´t compile. I beleave what I have
problems with the concept of the arrays and the pointers:

Luckily you posted an example of what you were
wanting to do. Otherwise, I don't think I would have been
able to parse your broken english with much success.
#include <stdio.h>
char composefile(void);
main ()

int main(void)
{
char uno[6];
uno = composefile();

uno is an unmodifiable l-value(can be an r-value too)
So you cannot assign anything to ``uno''
printf("%s", uno);
}

char composefile(void)
{
char KK[]="hola";

The KK array currently has automatic storage duration.
Its lifetime ends when the end of this function is reached.
return (&KK);

Even if you were able to assign to the ``uno'' array
This would fail, as it would be:
1) Incompatible types in assignment.
2) Conflicting types for function declaration of ``composefile''.

``&KK'' yields the address of an array, which makes the expression type
pointer to array of 5 char;
}

Someone can say me how to do this.

Yes, change ``char uno[6];'' to ``char *uno''
Change the prototype of ``composefile'' to:
char *composefile(void);
Ensure the definition of ``composefile'' corresponds to the prototype.

In ``composefile'', change the declaration of ``KK'' to:
static char KK[]="hola";

and return ``KK'' not ``&KK''; since the address ``KK''
contains will be yielded due to value context.
 
D

Dan Pop

In said:
Or if using static, it is the same as &KK is a syntax error.

There is nothing *syntactically* wrong with &KK. It is a constraint
violation (inherited from 6.5.16.1 Simple assignment) and a diagnostic
is required.
You could use &KK[0] but that is confusing.

It's not confusing at all, it's usually an indication that the coder
wasn't an experienced C programmer and can't be trusted to know what he
was doing.

Dan
 
D

Dan Pop

In said:
char composefile(void)
{
char KK[]="hola";
return (&KK);
}

Your array KK cease to exist once composefile() returns, so the pointer
you return isn't valid.

#define BUFFER_LENGTH 5
#include <stdio.h>
#include <string.h>

void composefile(char *);

int main(void) {
char buffer[BUFFER_LENGTH];
composefile(buffer);
printf("%s",buffer);
}

void composefile(char *b) {
char KK[] = "hola";
strncpy(b,KK,BUFFER_LENGTH);
}

You don't want to use strncpy here. If "hola" gets replaced by "hello",
composefile() no longer "returns" a string to its caller. strncat is
what you want:

*b = 0, strncat(b, KK, BUFFER_LENGTH - 1);

The code may still not behave as expected, but there is no undefined
behaviour when the caller attempts to use the string: the original string
merely gets truncated.

Of course, the same effect can be achieved with strncpy itself:

strncpy(b, KK, BUFFER_LENGTH), b[BUFFER_LENGTH - 1] = 0;

but it looks kinda silly and the assignment is redundant any time less
than BUFFER_LENGTH characters have been copied. For very large values
of BUFFER_LENGTH, there may be also a performance problem if the number
of copied characters is much less than BUFFER_LENGTH, because strncpy also
performs null padding, *always* writing BUFFER_LENGTH characters to the
destination (for purely historical reasons).

Despite its misleading prefix, strncpy is not to be used in a string
context. It was designed to manipulate a different data type, best called
"limited length string", where a terminating null character is not needed
if the string has the maximum allowed length. Of course, this maximum
length must be known to both caller and callee in the above example,
which could be rewritten like this:

int main(void)
{
char buffer[BUFFER_LENGTH];
composefile(buffer);
printf("%.*s\n", BUFFER_LENGTH, buffer);
return 0;
}

void composefile(char *b)
{
char KK[] = "hola";
strncpy(b, KK, BUFFER_LENGTH);
}

The only (relevant) difference from the original example is that
main() no longer relies on buffer to contain a C string after calling
composefile(): printf() is explicitly instructed not to display more
than BUFFER_LENGTH characters from buffer.

Dan
 

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,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top