Re: [go-nuts] cgo compile error: array type has incomplete element type

2019-05-05 Thread T L


On Sunday, May 5, 2019 at 1:09:47 AM UTC+8, Jan Mercl wrote:
>
> On Sat, May 4, 2019 at 6:53 PM T L > 
> wrote: 
>
> > BTW, the type bar is defined as 
> > 
> > typedef struct bar bar; 
>
> If struct bar is not yet defined at that point then it's an incomplete 
> type. So is bar. Not sure if calling an incomplete type 'defined' 
> makes sense. 
>
> However, this should be IMO ok 
>
> typedef struct bar bar; // bar is an incomplete type 
>
> struct bar { int foo; }; // bar is now a complete type 
>
> bar [2]x; // This declaration of x is now ok. 
>

Thanks for the explanations.

Long time not using c/c++. It looks it is a deference between c and c++.
The following program compiles ok for g++ and clang++, but not gcc and 
clang.

typedef struct bar bar;

int foo(bar bars[]) {
return 0;
}

int main() {
int x = 1;
bar* y = (bar*)
return foo(y);
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] cgo compile error: array type has incomplete element type

2019-05-04 Thread Jan Mercl
On Sat, May 4, 2019 at 6:53 PM T L  wrote:

> BTW, the type bar is defined as
>
> typedef struct bar bar;

If struct bar is not yet defined at that point then it's an incomplete
type. So is bar. Not sure if calling an incomplete type 'defined'
makes sense.

However, this should be IMO ok

typedef struct bar bar; // bar is an incomplete type

struct bar { int foo; }; // bar is now a complete type

bar [2]x; // This declaration of x is now ok.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] cgo compile error: array type has incomplete element type

2019-05-04 Thread T L
BTW, the type bar is defined as

typedef struct bar bar;

On Sunday, May 5, 2019 at 12:10:50 AM UTC+8, T L wrote:
>
> The FAQ, http://c-faq.com/aryptr/aryptrparam.html and 
> http://c-faq.com/aryptr/aryparmsize.html
> says "bool foo(bar bars[2]);" <=> "bool foo(bar bars[]);" <=> bool 
> foo(bar* bars);
> So I think if "bool foo(bar* bars);" compiles ok, then should also "bool 
> foo(bar bars[2]);. 
>
> On Sunday, May 5, 2019 at 12:05:08 AM UTC+8, Jan Mercl wrote:
>>
>> On Sat, May 4, 2019 at 2:15 PM T L  wrote: 
>>
>> > But it is a pointer parameter, though it looks like an array. 
>>
>> Yes, a pointer to an incomplete type. The compiler thus does not know 
>> how to compute address of bars[x] as that translates to bars + 
>> x*sizeof(bar). 
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] cgo compile error: array type has incomplete element type

2019-05-04 Thread T L
The FAQ, http://c-faq.com/aryptr/aryptrparam.html and 
http://c-faq.com/aryptr/aryparmsize.html
says "bool foo(bar bars[2]);" <=> "bool foo(bar bars[]);" <=> bool foo(bar* 
bars);
So I think if "bool foo(bar* bars);" compiles ok, then should also "bool 
foo(bar bars[2]);. 

On Sunday, May 5, 2019 at 12:05:08 AM UTC+8, Jan Mercl wrote:
>
> On Sat, May 4, 2019 at 2:15 PM T L > 
> wrote: 
>
> > But it is a pointer parameter, though it looks like an array. 
>
> Yes, a pointer to an incomplete type. The compiler thus does not know 
> how to compute address of bars[x] as that translates to bars + 
> x*sizeof(bar). 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] cgo compile error: array type has incomplete element type

2019-05-04 Thread Jan Mercl
On Sat, May 4, 2019 at 2:15 PM T L  wrote:

> But it is a pointer parameter, though it looks like an array.

Yes, a pointer to an incomplete type. The compiler thus does not know
how to compute address of bars[x] as that translates to bars +
x*sizeof(bar).

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] cgo compile error: array type has incomplete element type

2019-05-04 Thread T L


On Saturday, May 4, 2019 at 7:07:37 PM UTC+8, Jan Mercl wrote:
>
> On Sat, May 4, 2019 at 12:57 PM T L > 
> wrote: 
> > 
> > 
> > In one of my cgo project, there is a c API (a c++ wrapper) like 
> > 
> > bool foo(bar bars[2]); 
> > 
> > It fails to compile with error message: 
> > 
> > error: array type has incomplete element type 'bar' {aka 'struct bar'} 
>
> Incomplete type has uknown size. You must define struct bar before 
> using the typedef in the array declarator. 
>

But it is a pointer parameter, though it looks like an array. 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] cgo compile error: array type has incomplete element type

2019-05-04 Thread Jan Mercl
On Sat, May 4, 2019 at 12:57 PM T L  wrote:
>
>
> In one of my cgo project, there is a c API (a c++ wrapper) like
>
> bool foo(bar bars[2]);
>
> It fails to compile with error message:
>
> error: array type has incomplete element type 'bar' {aka 'struct bar'}

Incomplete type has uknown size. You must define struct bar before
using the typedef in the array declarator.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.