Searching \ for '[OT] Programming CCS pointer and struct problems' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devprogs.htm?key=programming
Search entire site for: 'Programming CCS pointer and struct problems'.

Exact match. Not showing close matches.
PICList Thread
'[OT] Programming CCS pointer and struct problems'
2000\06\07@003749 by Damon Hopkins

flavicon
face
I'm using the CCS compiler and have hit a few walls.

Is there a way that I can have a pointer to a structure defined
before the structure is defined..
Example:

struct header {
       struct body *body_ptr;
};

struct foot {
       struct body *body_ptr;
};

struct body {
       struct header head;
       struct footer foot;
};

this doesn't compile because the structure body if not defined when the
structure header is defined. I can't put the body structure first
because it contains the header structure.


also is there a way I can implement a pointers to functions I was
thinking of trying the #ORG directive for the functions (could get ugly)
and trying some #asm routines to go straight there but I want to know if
anyone else has figured out a way to do it..

                       Thanks,

                                       Damon Hopkins

2000\06\07@011127 by Dale Botkin

flavicon
face
I'm guessing, since I probably know less C than you do, but can you
prototype a structure like you can a function?  Like this:

struct body;
struct header {
       struct body *body_ptr;
};

struct foot {
       struct body *body_ptr;
};

struct body {
       struct header head;
       struct footer foot;
};

I know I had to do that with functions.  Unfortunately I don't know squat
about structures, having never needed to use them.  Heck, I just learned
about switch() the other day!

Dale
---
The most exciting phrase to hear in science, the one that heralds new
discoveries, is not "Eureka!" (I found it!) but "That's funny ..."
               -- Isaac Asimov

2000\06\07@025202 by AERTS PETER

flavicon
face
Just try this :

> -----Original Message-----
> From: Damon Hopkins [spam_OUTmdhopkinTakeThisOuTspamUNITY.NCSU.EDU]
> Sent: woensdag 7 juni 2000 6:19
> To: .....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU
> Subject: [OT] Programming CCS pointer and struct problems
>
>
> I'm using the CCS compiler and have hit a few walls.
>
> Is there a way that I can have a pointer to a structure defined
> before the structure is defined..
> Example:
>
/* Try this : */
typedef struct body   *body_ptr;
typedef struct header *header_ptr;
typedef struct footer *footer_ptr;

> struct header {
         body_ptr body;
> };
>
> struct footer {
         body_ptr body;
> };
>
> struct body {
         header_ptr header;
         footer_ptr footer;
> };

This should do it.

About Function pointers :
{Quote hidden}

/*
  what about :
  typedef returntype (*FunctionTypeName)(ArgType,ArgType,ArgType.....);
  eg :
*/

typedef int *(FuncReturningIntFromString)(char *);

/* And use it like this (just an example ) */
int TestFunction(char *string);
int TestFunction(char *string)
{
 int i;
 i = 0; /* just ot be explicit */

 while (*string)
 {
   i += (*string++);
 }
 return(i);
}

void Test ( FuncReturningIntFromString Function , char *String )
{
 int Result;
 /* And call */

 Result = Function(String);

 .......
}

So :

Test(TestFunction,"123");
should call TestFunction() with "123" as parameter and return an int to
Test().

Peter
ON1DNT

2000\06\07@064449 by Bob Ammerman

picon face
Assuming CCS is ANSI conforming:

struct body;    // forward declaration (this is the magic!)

struct header

  struct body *body_ptr;
};

struct footer {
 struct body *body_ptr;
};

struct body {
   struct header head;
   struct footer foot;
};

Also: assume you need a pointer to a function returning int and taking two
ints as arguments:

Just start with a line that looks like a prototype of a function of the type
you want:

int function(int,int);

Replace the function name as follows:

int (*function_ptr)(int,int);

Now make a typedef out of it:

typedef int (*function_ptr)(int,int);

This now says that any variable of type function_ptr will be a pointer to a
function taking two ints and returning an int.

We can use it like this:

int afunc(int,int)
{
   ....
}

int bfunc(int,int)
{
  ...
}

void some_piece_of_code()
{
   function_ptr fptr;

   if (....)
       fptr = afunc;
   else
       fptr = bfunc;

   ...

   j = (*fptr)(1,2)    ....
}

Note that you must declare a separate pointer type for each different
function signature.

Bob Ammerman
RAm Systems
(high function, high performance, low-level software)

2000\06\07@100659 by Michael Damon Hopkins

flavicon
face
I forgot about prototyping structures.. Man.. ok I'll try all that out..
I knew there was a way...

               Thanks..

                       Damon Hopkins

2000\06\07@140639 by Damon Hopkins

flavicon
face
so far EVERYTHING that has been mentioned does not work
if I prototype a struct the compiler tells me that it expects a {

when I try the various function pointer suggestions I either get that
the compiler
a.) "expects an identifier"
or
b.) improper use of a "function identifier"

I guess I am SOL.

                       Damon Hopkins

2000\06\07@145841 by rleggitt

picon face
SOL? Naa, you just have to go around. In this case I might try something
like:

struct
{
 void *p;
} some;

struct
{
 int x;
} thing;

 ...
 (struct thing *)some.p = &thing;

 if ((struct thing *)(some.p)->x)
 ...

Although please note if I did try this, I would probably also have to
diddle with the exact syntax to get it to compile right.

On Wed, 7 Jun 2000, Damon Hopkins wrote:

{Quote hidden}

2000\06\07@162555 by Bob Ammerman

picon face
Sounds like you don't have a very compliant compiler!

You can try this:

struct h {
   void  *body;
};

struct f {
  void *body;
};

struct b {
 struct h *header;
 struct f *footer;
};

Then, when you need to refer to the body, do something like this:

struct h my_hdr;

((struct b *) my_hdr.body)->whatever;

which will take the void pointer and cast it to the correct type before
using it.

Bob Ammerman
RAm System
(high performance, high function, low level software)

2000\06\07@175950 by Sergio Masci

flavicon
picon face
I know this wont sound very nice but it should get round the need to
constantly cast your pointers. Try overlaying the definition of your
structure onto another almost identical one that does not have forward
refernces.

You will need to define your problem structure twice but you wont
actually be using twice as much space.


       /* note that struct xb and struct b must be identical
          except for the forward references */
       struct xb {
         void *header;
         void *footer;

         int abc_1;
         int abc_2;
         int abc_3;
       };

       struct h {
           struct xb *body;
       };

       struct f {
          struct xb *body;
       };

       /* note that struct xb and struct b must be identical
          except for the forward references */
       struct b {
         struct h *header;
         struct f *footer;

         int abc_1;
         int abc_2;
         int abc_3;
       };


then you should be able to do things like

       struct h my_hdr;

       x = my_hdr.body->abc_1;

You may need to do a coercion here, but it beats doing
one everywhere
i.e. this
       my_hdr = my_hdr.body->header;
may need to be written as:
       my_hdr = (struct b *)(my_hdr.body->header);

NOTE: Don't ever declare/make/allocate a structure xb only ever of use b


Irrespective of all this, forward refernces to a structure shouldn't
cause a problem when they are being used to declare pointers, since all
the compiler needs to do is allocate space for a pointer to a
structure. The details concerning the layout of the structure are completely
irrelevant until you try to access the members of the structure or allocate
space for it.

--
Sergio Masci


{Quote hidden}

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...