Pointer and integer addition

Michael N Johnston id8rld06 at serss0.fiu.edu
Sun Mar 31 14:48:13 AEST 1991


>struct foo {
>unsigned short recsize;
>unsigned short num;
>char info [24];
>byte flags;
>char filename[1]
>};
>Where, foo.filename is a placeholder for a variable length string.

>After I process one record, I'd like to just increment the pointer to
>point to the next record.  What I'd like to do is:
>fooptr += fooptr->recsize;

>However, the compiler I'm using increments the pointer by
>recsize * sizeof (struct foo).   This is probably correct.

If you are declaring foo_ptr as
struct foo *foo_ptr;

then this is indeed correct operation. What you need to do to incrment
foo_ptr to point to the next record is (assuming foo_ptr is pointing
to an array of struct foo):
++foo_ptr;

If you plan to store a char array in filename DON'T. Use char *filename
and point this to your actual array. Using your origional declaration
and doing something like strcpy(foo_ptr->filename, "abcde") will lead
to disaster.

Good Luck,
Mike
--
====================================================================

Michael Johnston
id8rld06 at serss0.fiu.edu or



More information about the Comp.lang.c mailing list