return a char array from a function

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

  1. Fernando

    Fernando Guest

    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
    Fernando, Nov 5, 2003
    #1
    1. Advertising

  2. Fernando

    Allan Bruce Guest

    "Fernando" <> wrote in message
    news:dB6qb.257$...
    > 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;

    > }
    >
    > Someone can say me how to do this.
    >
    > Thanks,
    > Fernando
    >
    >
    Allan Bruce, Nov 5, 2003
    #2
    1. Advertising

  3. Fernando

    Allan Bruce Guest

    > > 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
    Allan Bruce, Nov 5, 2003
    #3
  4. On Wed, 05 Nov 2003 13:06:20 +0100, Fernando wrote:

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


    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
    Nils Petter Vaskinn, Nov 5, 2003
    #4
  5. Fernando

    pete Guest

    Fernando wrote:
    >
    > 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;
    }

    --
    pete
    pete, Nov 5, 2003
    #5
  6. Fernando

    j Guest

    "Fernando" <> wrote in message
    news:dB6qb.257$...
    > 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.

    > Thanks,
    > Fernando
    >
    >
    j, Nov 5, 2003
    #6
  7. Fernando

    Dan Pop Guest

    In <boaplt$bt4$2surf.net> "Allan Bruce" <> writes:

    >> > return (&KK);

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

    >
    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Nov 5, 2003
    #7
  8. Fernando

    Dan Pop Guest

    In <> "Nils Petter Vaskinn" <> writes:

    >On Wed, 05 Nov 2003 13:06:20 +0100, Fernando wrote:
    >
    >> 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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Nov 5, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    524
    Bryan Donlan
    Aug 26, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,002
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    719
  4. davidb
    Replies:
    0
    Views:
    751
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,529
    Default User
    Sep 1, 2006
Loading...

Share This Page