Wednesday, 15 September 2010

structs in C and .h files -



structs in C and .h files -

i confused on structs in c. trying create .h file has structs using. created structs.h

#include <ucontext.h> #include <string.h> #include <stdio.h> #include <stdlib.h> struct tcb_t; typedef struct { struct tcb_t * next; struct tcb_t * previous; ucontext_t context; int val; }tcb_t;

my tcb.h file

#include "structs.h" int count =0; struct tcb_t *runq = null; struct tcb_t *ptr = null; void init_tcb (struct tcb_t *tcb, void *function, void *stackp, int stack_size, int *arg) { memset(tcb, '\0', sizeof(struct tcb_t)); getcontext(&tcb->context); tcb->context.uc_stack.ss_sp = stackp; tcb->context.uc_stack.ss_size = (size_t)stack_size; makecontext(&tcb->context, function, 1, arg); }

when run next errors.

description resource path location type field 'ss_size' not resolved tcb.h /projthree/src line 14 semantic error description resource path location type field 'ss_sp' not resolved tcb.h /projthree/src line 13 semantic error description resource path location type field 'uc_stack' not resolved tcb.h /projthree/src line 13 semantic error description resource path location type field 'uc_stack' not resolved tcb.h /projthree/src line 14 semantic error description resource path location type symbol 'null' not resolved tcb.h /projthree/src line 6 semantic error description resource path location type symbol 'null' not resolved tcb.h /projthree/src line 7 semantic error

if move struct fron structs.h tcb.h errors go away. why , shouldn't tcb.h have access structs in structs.h because included "structs.h" on top of page?

the problem you've declared there struct tcb_t somewhere, , you've defined typedef name tcb_t tagless (anonymous) struct type, you've not defined type struct tcb_t.

struct tcb_t; // there is, somewhere, type struct tcb_t typedef struct // anonymous struct, not struct tcb_t { struct tcb_t * next; struct tcb_t * previous; ucontext_t context; int val; } tcb_t; // typedef anonymous struct

you need write either this:

typedef struct tcb_t tcb_t; struct tcb_t { tcb_t *next; // optionally struct tcb_t tcb_t *previous; // optionally struct tcb_t ucontext_t context; int val; };

or this:

typedef struct tcb_t { struct tcb_t *next; struct tcb_t *previous; ucontext_t context; int val; } tcb_t;

both end struct tcb_t , plain type tcb_t alias struct tcb_t.

beware, _t suffix officially reserved utilize implementation (the compiler , supporting libraries). might run problems using (but won't until uncomfortably late alter name).

and reason compilation errors compiler hasn't been told struct tcb_t contains, can't access context fellow member of it, , hence not fields within context member.

c struct

No comments:

Post a Comment