0

If I declare a function parameter as myfunc(char (*p) [10]) what is the correct syntax for returning said pointer p ?

char *myfunc(char (*p) [10]) {
    /* do something */
    return (*p);
}

compiles and appears to work but it doesn't look correct (i.e., it doesn't seem to suggest that the pointer returned necessarily is a pointer to an array of size 10).

9
  • 1
    Your question does not make sense. You say you want the function myfunc to simply return its function argument, but this would only make sense if the argument and the return value were of the same type. Are you asking how to cast a pointer from char (*)[10] to char *? Commented Jun 22, 2021 at 23:46
  • 1
    No. I would like to know the correct function definition to imply that the function returns a pointer to an array of size 10. The example I gave works but does not make it clear that the pointer returned is to an array of size 10. In short, I do not know how to define the return type of the function to reflect this. Commented Jun 22, 2021 at 23:58
  • First of all, you need to decide whether you are returning p or *p since those are two very different things. *p is in fact a char *, so your function is correct as is. Commented Jun 23, 2021 at 0:00
  • 2
    char (*myfunc(char (*p)[10]))[10] is a function that takes a pointer-to-an-array and returns a pointer-to-an-array. The absolutely horrible syntax is the reason that you won't often see this. What are you actually trying to accomplish? Commented Jun 23, 2021 at 0:03
  • 1
    This is either a very academic question or it is an XY problem. My guess is it is the latter. Commented Jun 23, 2021 at 0:10

2 Answers 2

3

char x declares a character.

char x[10] declares an array of ten characters.

char (*x)[10] declares a pointer to an array of ten characters.

char (*x())[10] declares a function that returns a pointer to an array of ten characters.

char (*x(char (*p)[10]))[10] declares a function that takes a pointer to an array of ten characters and returns a pointer to an array of ten characters.

Sign up to request clarification or add additional context in comments.

1 Comment

with typedef a bit more clear: typedef char (* ptr_to_char_arr_10) [10]; ptr_to_char_arr_10 (*f_returning_arr_p)(void) ; ptr_to_char_arr_10 (*f_receiving_and_returning_arr_p)(ptr_to_char_arr_10) ;
1

typedef is one of your friends. Declare the type alias pointer to your required array:

typedef char (* ptr_to_char_arr_10) [10];

Now use it for myfunc:

 ptr_to_char_arr_10  myfunc(ptr_to_char_arr_10 p) {
    /* do something */
     *p[0] = 42;
    return p;
}

And now use it to use myfunc:

int main ( void )
{
    char char_arr_10 [10] = {0};

    ptr_to_char_arr_10 arrp = myfunc(&char_arr_10) ;

    assert( *arrp[0] == 42 );

    return EXIT_SUCCESS ;
}

Godbolt

Pointer to the array is a powerful concept. For further inspiration perhaps see it here, used with Variably Modified Types and heap allocation.

Bonus

The question is actually titled: "c programming language fixed-size array". Arrays as function argument actually can be declared with a "fixed" size.

// "fixed " array argument declaration
static void  dbj_assign(
        char val_, 
        // run time size of the array arg.
        const int count_, 
        // array argument of a minimal required size
        char char_arr [static count_]
     ) 
    {
        /* char_arr can not be null here*/
        /* do something */
        char_arr[0] = val_;
    }  

That char_arr argument is also a Variably Modified Type (VMT), with minimal size required also declared. Usage is in the same Godbolt.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.