array defenition...please advise

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Freaks
Again it looks like i need you help :?
Well here it is..........

I declared an unsigned int array like this:
unsigned int data_base_holder[5];

now i need to load the values of the array according to a case value
I will try to explain here is what i wrote:

case (random_number)
{
case 1:
data_base_holder={1124,1080,1686,1080,0};
break;

default:/*anything else then the above*/
lcd_gotoxy(0,1);
lcd_putsf("Wrongggggg");
delay_ms(200);
break;

As you can figure it .....i got an Error massage from the Code Vision
the massage that i am recieving is :L16 must be lvalue.

What am i doing wrong and how can i load those values to the array and add some more cases to this routine

Please advise :shock:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tzif wrote:
data_base_holder={1124,1080,1686,1080,0};

data_base_holder[]={1124,1080,1686,1080,0}; 

remember, the array segment begins at array[0], and is given an lvalue, or essentially a pointer address.

If you wanted to update only a part of the arrary:

data_base_holder[2] = 3; 

is like coding:

data_base_holder[]={1124,1080,3,1080,0}; 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Sonos
Thank you for your quixck replay
I have tried that and i dont get the massage that i recieved before
But insted i keep on getting the massage "L16 Invalid expression"

I hope that you have another Rabbit from the hat :wink:

Thank you any way :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you can only use that type of assignment shortcut in the initialization, you cannot use it in code. for example:

int myarray[] = {1,2,3,4,5,6};

will work

int myarray[6];
.
.
.
myarray = {1,2,3,4,5,6};

Will not work. This is because it is trying to change the value of the array pointer itself.

The solution here is to A: manually set each entry or B: use a function like memcpy, or memset to load the array space in RAM with values stored elsewhere.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It would help if you post all your code, and sorry for the error I posted.

as for array information, check here...

http://publications.gbdirect.co....

and for the kissing cousin of arrays (pointers) check here....

http://publications.gbdirect.co....

Quote:
5.3.2. Arrays and pointers

Array elements are just like other variables: they have addresses.

int ar[20], *ip;
ip = &ar[5];
*ip = 0; /* equivalent to ar[5] = 0; */

The address of ar[5] is put into ip, then the place pointed to has zero assigned to it. By itself, this isn't particularly exciting. What is interesting is the way that pointer arithmetic works. Although it's simple, it's one of the cornerstones of C.

Adding an integral value to a pointer results in another pointer of the same type. Adding n gives a pointer which points n elements further along an array than the original pointer did. (Since n can be negative, subtraction is obviously possible too.) In the example above, a statement of the form

*(ip+1) = 0;

would set ar[6] to zero, and so on. Again, this is not obviously any improvement on ?ordinary? ways of accessing an array, but the following is.

int ar[20], *ip;
for(ip = &ar[0]; ip < &ar[20]; ip++)
*ip = 0;

the last example in the quote would allow you to zip through the entire array in main() and refresh the database.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi freaks
Glitch you did it again
Thank you its working
As for sonos.......Great site to look for help
Thank you again for your care and usefl help
I hope tat one day i will be the one to help you guys

Thanks :D