Returning an array using C

前端 未结 8 1092
你的背包
你的背包 2020-11-21 04:45

I am relatively new to C and I need some help with methods dealing with arrays. Coming from Java programming, I am used to being able to say int [] method()in o

相关标签:
8条回答
  • 2020-11-21 05:17

    How about this deliciously evil implementation?

    array.h

    #define IMPORT_ARRAY(TYPE)    \
        \
    struct TYPE##Array {    \
        TYPE* contents;    \
        size_t size;    \
    };    \
        \
    struct TYPE##Array new_##TYPE##Array() {    \
        struct TYPE##Array a;    \
        a.contents = NULL;    \
        a.size = 0;    \
        return a;    \
    }    \
        \
    void array_add(struct TYPE##Array* o, TYPE value) {    \
        TYPE* a = malloc((o->size + 1) * sizeof(TYPE));    \
        TYPE i;    \
        for(i = 0; i < o->size; ++i) {    \
            a[i] = o->contents[i];    \
        }    \
        ++(o->size);    \
        a[o->size - 1] = value;    \
        free(o->contents);    \
        o->contents = a;    \
    }    \
    void array_destroy(struct TYPE##Array* o) {    \
        free(o->contents);    \
    }    \
    TYPE* array_begin(struct TYPE##Array* o) {    \
        return o->contents;    \
    }    \
    TYPE* array_end(struct TYPE##Array* o) {    \
        return o->contents + o->size;    \
    }
    

    main.c

    #include <stdlib.h>
    #include "array.h"
    
    IMPORT_ARRAY(int);
    
    struct intArray return_an_array() {
        struct intArray a;
        a = new_intArray();
        array_add(&a, 1);
        array_add(&a, 2);
        array_add(&a, 3);
        return a;
    }
    
    int main() {
        struct intArray a;
        int* it;
        int* begin;
        int* end;
        a = return_an_array();
        begin = array_begin(&a);
        end = array_end(&a);
        for(it = begin; it != end; ++it) {
            printf("%d ", *it);
        }
        array_destroy(&a);
        getchar();
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 05:19

    C's treatment of arrays is very different from Java's, and you'll have to adjust your thinking accordingly. Arrays in C are not first-class objects (that is, an array expression does not retain it's "array-ness" in most contexts). In C, an expression of type "N-element array of T" will be implicitly converted ("decay") to an expression of type "pointer to T", except when the array expression is an operand of the sizeof or unary & operators, or if the array expression is a string literal being used to initialize another array in a declaration.

    Among other things, this means that you cannot pass an array expression to a function and have it received as an array type; the function actually receives a pointer type:

    void foo(char *a, size_t asize)
    {
      // do something with a
    }
    
    int bar(void)
    {
      char str[6] = "Hello";
      foo(str, sizeof str);
    }
    

    In the call to foo, the expression str is converted from type char [6] to char *, which is why the first parameter of foo is declared char *a instead of char a[6]. In sizeof str, since the array expression is an operand of the sizeof operator, it's not converted to a pointer type, so you get the number of bytes in the array (6).

    If you're really interested, you can read Dennis Ritchie's The Development of the C Language to understand where this treatment comes from.

    The upshot is that functions cannot return array types, which is fine since array expressions cannot be the target of an assignment, either.

    The safest method is for the caller to define the array, and pass its address and size to the function that's supposed to write to it:

    void returnArray(const char *srcArray, size_t srcSize, char *dstArray, char dstSize)
    {
      ...
      dstArray[i] = some_value_derived_from(srcArray[i]);
      ...
    }
    
    int main(void)
    {
      char src[] = "This is a test";
      char dst[sizeof src];
      ...
      returnArray(src, sizeof src, dst, sizeof dst);
      ...
    }
    

    Another method is for the function to allocate the array dynamically and return the pointer and size:

    char *returnArray(const char *srcArray, size_t srcSize, size_t *dstSize)
    {
      char *dstArray = malloc(srcSize);
      if (dstArray)
      {
        *dstSize = srcSize;
        ...
      }
      return dstArray;
    }
    
    int main(void)
    {
      char src[] = "This is a test";
      char *dst;
      size_t dstSize;
    
      dst = returnArray(src, sizeof src, &dstSize);
      ...
      free(dst);
      ...
    }
    

    In this case, the caller is responsible for deallocating the array with the free library function.

    Note that dst in the above code is a simple pointer to char, not a pointer to an array of char. C's pointer and array semantics are such that you can apply the subscript operator [] to either an expression of array type or pointer type; both src[i] and dst[i] will access the i'th element of the array (even though only src has array type).

    You can declare a pointer to an N-element array of T and do something similar:

    char (*returnArray(const char *srcArr, size_t srcSize))[SOME_SIZE]
    {
      char (*dstArr)[SOME_SIZE] = malloc(sizeof *dstArr);
      if (dstArr)
      {
        ...
        (*dstArr)[i] = ...;
        ...
      }
      return dstArr;
    }
    
    int main(void)
    {
      char src[] = "This is a test";
      char (*dst)[SOME_SIZE];
      ...
      dst = returnArray(src, sizeof src);
      ...
      printf("%c", (*dst)[j]);
      ...
    }
    

    Several drawbacks with the above. First of all, older versions of C expect SOME_SIZE to be a compile-time constant, meaning that function will only ever work with one array size. Secondly, you have to dereference the pointer before applying the subscript, which clutters the code. Pointers to arrays work better when you're dealing with multi-dimensional arrays.

    0 讨论(0)
  • 2020-11-21 05:19

    In your case, you are creating an array on the stack and once you leave the function scope, the array will be deallocated. Instead, create a dynamically allocated array and return a pointer to it.

    char * returnArray(char *arr, int size) {
        char *new_arr = malloc(sizeof(char) * size);
        for(int i = 0; i < size; ++i) {
            new_arr[i] = arr[i];
        }
        return new_arr;
    }
    
    int main() {
    
        char arr[7]= {1,0,0,0,0,1,1};
        char *new_arr = returnArray(arr, 7);
    
        // don't forget to free the memory after you're done with the array
        free(new_arr);
    
    }
    
    0 讨论(0)
  • 2020-11-21 05:21

    Your method will return a local stack variable that will fail badly. To return an array, create one outside the function, pass it by address into the function, then modify it, or create an array on the heap and return that variable. Both will work, but the first doesn't require any dynamic memory allocation to get it working correctly.

    void returnArray(int size, char *retArray)
    {
      // work directly with retArray or memcpy into it from elsewhere like
      // memcpy(retArray, localArray, size); 
    }
    
    #define ARRAY_SIZE 20
    
    int main(void)
    {
      char foo[ARRAY_SIZE];
      returnArray(ARRAY_SIZE, foo);
    }
    
    0 讨论(0)
  • 2020-11-21 05:22

    I am not saying that this is the best solution or a preferred solution to the given problem. However, it may be useful to remember that functions can return structs. Although functions cannot return arrays, arrays can be wrapped in structs and the function can return the struct thereby carrying the array with it. This works for fixed length arrays.

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
    
        typedef
        struct 
        {
            char v[10];
        } CHAR_ARRAY;
    
    
    
        CHAR_ARRAY returnArray(CHAR_ARRAY array_in, int size)
        {
            CHAR_ARRAY returned;
    
            /*
            . . . methods to pull values from array, interpret them, and then create new array
            */
    
            for (int i = 0;  i < size; i++ )
                returned.v[i] = array_in.v[i] + 1;
    
            return returned; // Works!
        } 
    
    
    
    
        int main(int argc, char * argv[])
        {
            CHAR_ARRAY array = {1,0,0,0,0,1,1};
    
            char arrayCount = 7;
    
            CHAR_ARRAY returnedArray = returnArray(array, arrayCount); 
    
            for (int i = 0; i < arrayCount; i++)
                printf("%d, ", returnedArray.v[i]);  //is this correctly formatted?
    
            getchar();
            return 0;
        }
    

    I invite comments on the strengths and weaknesses of this technique. I have not bothered to do so.

    0 讨论(0)
  • 2020-11-21 05:22

    You can do it using heap memory (through malloc() invocation) like other answers reported here, but you must always manage the memory (use free() function everytime you call your function). You can also do it with a static array:

    char* returnArrayPointer() 
    {
    static char array[SIZE];
    
    // do something in your array here
    
    return array; 
    }
    

    You can than use it without worrying about memory management.

    int main() 
    {
    char* myArray = returnArrayPointer();
    /* use your array here */
    /* don't worry to free memory here */
    }
    

    In this example you must use static keyword in array definition to set to application-long the array lifetime, so it will not destroyed after return statement. Of course, in this way you occupy SIZE bytes in your memory for the entire application life, so size it properly!

    0 讨论(0)
提交回复
热议问题