This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C51: a lot of values assignment

Hi,

In my C code, I need to assign a lot of values.
Like this:
a1,a2,... are variables as char.
v1,v2...,w1,w2...y1,y2 are constant (like 0x00, 0x01, ...etc)

if (i==0){
a1 = v1;
a2 = v2;
...
a50=v50;
} else if (i==2){
a1 = w1;
a2 = w2;
...
a50=w50;
} else if (i==3){
a1 = y1;
a2 = y2;
...
a50=y50;
} else {
}

I would like to minimize the code memory usage as possible.
Which coding style or method is suggested to implement it ?
Should I declare "array" or use " #define " ?
Please kindly provide suggestions. Thanks a lot!

Parents
  • "Is *a2 being an array that consists 50 elements"

    No!

    a2 is a pointer which points to the start of one of the constant arrays - so there is no copying of the data!

    Of course, in 'C', arrays & pointers are very closely related; eg, the indexing operator [] can be applied to a pointer:

    *a2 dereferences the pointer; ie it gives the pointed-to item - which is the 1st element of one of the constant arrays.

    a2[0] is equivalent to *a2.

    a2[1] gives the second element of one of the constant arrays.

    etc, etc,...

    This is 'C' textbook stuff - nothing specific to the 8051, Keil, C51, or embedded.

    c-faq.com/.../index.html

Reply
  • "Is *a2 being an array that consists 50 elements"

    No!

    a2 is a pointer which points to the start of one of the constant arrays - so there is no copying of the data!

    Of course, in 'C', arrays & pointers are very closely related; eg, the indexing operator [] can be applied to a pointer:

    *a2 dereferences the pointer; ie it gives the pointed-to item - which is the 1st element of one of the constant arrays.

    a2[0] is equivalent to *a2.

    a2[1] gives the second element of one of the constant arrays.

    etc, etc,...

    This is 'C' textbook stuff - nothing specific to the 8051, Keil, C51, or embedded.

    c-faq.com/.../index.html

Children
No data