how to assign structures in C18 compiler, MPLAB (ANSI C89)

1k views Asked by At

I am trying to assign values in main program.

typedef struct PointStructure 
{   unsigned char x;    
    unsigned char y; 
    unsigned char offset; 
    unsigned char page;
}point;

volatile far point p;
volatile far point AryP[5];

The only way I could assign values to the structure p in main program is

p.x=10;
p.y=20;
p.offset=0;
p.page=1;

This way it will take a lot of space and time for me to assign values to the arrays. Is there a better way to do this?

Following ways did not work for me..

p = {10, 20, 0, 1};

p = {.x = 10, .y=20, .offset=0, .page=1};
1

There are 1 answers

0
Jing Wen On
p = {10, 20, 0, 1};

p = {.x = 10, .y=20, .offset=0, .page=1};

The above method is incorrect, unless you use it as initialization:

volatile far point p = {.x = 10, .y=20, .offset=0, .page=1};

To assign value into a structure, the following is the only way:

p.x=10;
p.y=20;
p.offset=0;
p.page=1;

There is no any way to assign multiple members in the same time, unless you make a function to do it.

void assignValue(point *p, x, y, offset, page)
{
      p->x = x;
      p->y = y;
      p->offset = offset;
      p->page = page;
}

Therefore you can just assign the value like this:

assignValue(&p, 10, 20, 0, 1);