return a char array from a function

Discussion in 'C Programming' started by Fernando, Nov 5, 2003.

  1. Fernando

    Fernando Guest

    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.

    Fernando, Nov 5, 2003
    1. Advertisements

  2. Fernando

    Allan Bruce Guest

    you should specify that main returns an int - always
    This shoulg be:
    char *uno;
    Should return char* i.e.
    char *composefile();
    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")
    If using a pointer, then just:
    return KK;
    Allan Bruce, Nov 5, 2003
    1. Advertisements

  3. Fernando

    Allan Bruce Guest

    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 Bruce, Nov 5, 2003
  4. 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];

    void composefile(char *b) {
    char KK[] = "hola";
    Nils Petter Vaskinn, Nov 5, 2003
  5. Fernando

    pete Guest

    #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;
    pete, Nov 5, 2003
  6. Fernando

    j Guest

    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.
    int main(void)
    uno is an unmodifiable l-value(can be an r-value too)
    So you cannot assign anything to ``uno''
    The KK array currently has automatic storage duration.
    Its lifetime ends when the end of this function is reached.
    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;
    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.
    j, Nov 5, 2003
  7. Fernando

    Dan Pop Guest

    There is nothing *syntactically* wrong with &KK. It is a constraint
    violation (inherited from Simple assignment) and a diagnostic
    is required.
    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 Pop, Nov 5, 2003
  8. Fernando

    Dan Pop Guest

    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];
    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 Pop, Nov 5, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads