baibo
baibo

Reputation: 458

function return pointer to array type in C

The following piece of C code compiles under gcc 4.7, on a Debian (run with gcc -c filename.c)

typedef int array_type[3];
int a[] = {1,2,3};
int* asd1(void){
    return a;
}
array_type* asd2(void){
    return &a;
}

Instead of using the typedef I want to use the actual type. However, if I replace array_type* with something like int*[3] it doesn't compile.

What should I replace array_type* with to make it semantically the same as above and compile correctly?

Upvotes: 1

Views: 108

Answers (3)

m13r
m13r

Reputation: 2731

Maybe have a look at this.
And as stated here you should not use such kind of typedef statements.

Upvotes: 1

haccks
haccks

Reputation: 106112

Try this:

int (*asd2(void))[3]{
     return &a;
}

Upvotes: 1

undur_gongor
undur_gongor

Reputation: 15954

int (*asd2(void))[3]{
    return &a;
}

see cdecl

Upvotes: 3

Related Questions