Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return char (*)[6] in c?

I want to sort array of string, which is array of array of characters in c, in alphabetical order.Here is the body of my function :-

char (*)[6] sort_strings ( char (*sptr) [6])
{

     //code.
     //return a pointer of type char (*)[6].

}

But this type of return type is not recognized by the compiler.It gives error saying:-

expected identifier or '(' before ')' token

So how do i return a pointer of type char (*)[6]? I have another question in mind, firstly see the main() as follows:-

int main(){

    char names[5][6] = {

            "tom",
            "joe",
            "adam"
    };

    char (*result)[6] = sort_strings (names);

    //code for printing the result goes here.

    return 0;
}

So my next question is that when i call sort strings (names) compiler is also giving me warning :-

initializing makes pointer from integer without a cast

So my questions are :-

1. How to return char(*)[6] from a function?

2. Why the compiler giving me warning when i call this function?

I am running this code on code blocks on windows.

like image 802
OldSchool Avatar asked Dec 24 '22 22:12

OldSchool


1 Answers

Function declarations look like variable declarations, except that the variable name is replaced by the function name and arguments. So:

// asdf is a pointer to an array of 6 chars
char (*asdf)[6];

// sort_strings is a function returning a pointer to an array of 6 chars
// (and with an argument which is a pointer to an array of 6 chars)
char (*sort_strings ( char (*sptr)[6] )) [6];
like image 172
user253751 Avatar answered Dec 29 '22 00:12

user253751