0

Current main() below, which is working now, is used to modify the size & values of an input array, and to generate an output array.

int main()
{
    unsigned char input_text[] = {0x00, 0x01, 0x02 ....};
    int ilength = sizeof(input_text)/sizeof(input_text[0]);

    int *olength;
    olength = &ilength;
    unsigned char* output_text = (unsigned char*)malloc(sizeof(unsigned char)*(*olength));

    int option = 1;
    switch(option)
    {   // array size and some elements values will be changed below
     case 1:
        change_output_method_1(&output_text, input_text, ilength, olength);
        // void change_output_method_1(unsigned char** output, unsigned char* input, int inlen, int *olen);
    }

    return 0;
}

Now, I want to revise above main() as a call function named change( ), which reads input_text and option values from new main( ), and return output_text value to new main().

Of course, create another new main( ) to call change( ).

unsigned char* change(unsigned char input_text, int option)
{
    int ilen = sizeof(input_text)/sizeof(input_text[0]);

    (same as before)

    return output_text;
}

int main()
{
    int opt = 1;
    unsigned char input[] = {same};

    unsigned char output[] = change(input, opt);
    // IMPORTANT: output[] size is unknown before receiving the return value from change( )
}

I meet some errors when define char arrays because of pointer issues. How to revise the new code?

TJCLK
  • 183
  • 12
  • Please see [Why isn't the size of an array parameter the same as within main?](https://stackoverflow.com/questions/1975128/why-isnt-the-size-of-an-array-parameter-the-same-as-within-main) – Weather Vane Jul 25 '19 at 11:22

2 Answers2

3

This does not work:

unsigned char output[] = change(input, opt);

...

unsigned char* change(unsigned char input_text, int option)

You cannot pass an array to your change function. You have to pass a pointer to the array (technically exact: to the first element of the array), together with the size:

unsigned char output[] = change(input, sizeof(input), opt);

...

unsigned char* change(unsigned char *input_text, int ilen, int option) {

Furthermore, you cannot return an array, either. You can for example return a dynamically allocated buffer and pass the size of the output buffer in an output parameter:

int output_size;
char *output = change(input, sizeof(input), &output_size, opt);

...

unsigned char* change(unsigned char *input_text, int ilen, int *olen, int option) {
    char *ret = malloc(SIZE);
    *olen = SIZE;

    return ret;
}

There are of course other possibilities, it depends on the exact situation.

Ctx
  • 18,090
  • 24
  • 36
  • 51
3

You need the following

unsigned char * change( const unsigned char *input_text, size_t n, int option )
{
    unsigned char *output_text = malloc( n * sizeof(unsigned char) );

    (same as before)

    return output_text;
}

and call it like

unsigned char *output_text = change( input_text, 
                                     sizeof( input_text ) / sizeof( *input_text ), 
                                     option );

Arrays used as function arguments are implicitly converted to pointers to their first elements.

And a function parameter declared as an array is adjusted by the compiler to pointer to the array element type.

That is within the function input_text is a pointer. You have to pass explicitly the size of the array.

If the size of the output array can differ from the size of the input array then the approach can be the following.

Within main you declare a pointer

unsigned char *output_text = NULL;

The function declaration in this case will look like

size_t change( const unsigned char *input_text, unsigned char **output_text, size_t n, int option )
{
    size_t size = /* some initializer expression */;
    *output_text = malloc( size * sizeof(unsigned char) );

    (same as before)

    return size; // where size is the size of the array output_text
}

and the function will be called like

size_t olength = change( input_text, 
                         sizeof( input_text ) / sizeof( *input_text ),
                         &output_text,
                         option );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335