Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

should gcc give warning for this case?

5 views
Skip to first unread message

Zhu Qun-Ying

unread,
Apr 26, 2012, 4:26:25 PM4/26/12
to
For the simple program, forward.c:

#include <stdio.h>

typedef struct _item_st {
int data;
struct item_st *next;
} item_t;

int
main (int argc, char *argv[])
{
item_t head = {0, NULL};

head.data = 1;

printf ("head.data: %d ptr: %p\n", head.data, (void *)head.next);
return 0;
}

I compile with gcc 4.7.0 with:
gcc -Wall -pedantic -std=c99 forward.c

No warning at all. Should gcc warn about the *next pointer points to an
unknown structure? I know it is allow by the standard, but most of the
case, it indicates some error in the code.

Cong Wang

unread,
May 8, 2012, 9:41:45 AM5/8/12
to
On Thu, 26 Apr 2012 at 20:26 GMT, Zhu Qun-Ying <zhu.q...@gmail.com> wrote:
>
> No warning at all. Should gcc warn about the *next pointer points to an
> unknown structure? I know it is allow by the standard, but most of the
> case, it indicates some error in the code.

Why? What errors? As long as you don't defreference (or increase) it,
the type it points to doesn't have to be known, it just a pointer which
is same with 'unsigned long' at bits level, on most of the arch.

Actually, there is a perfect use-case of this, which is called opaque
pointer, see: http://en.wikipedia.org/wiki/Opaque_pointer.
0 new messages