|
|
View previous topic :: View next topic |
Author |
Message |
cjusto
Joined: 26 Apr 2006 Posts: 38 Location: Portugal
|
changing an array in the main |
Posted: Sat May 13, 2006 10:16 am |
|
|
hi everybody!
i have a doubt.
when i declare an array like this (global variable):
Code: |
BYTE in_buffer[10] = {0,0,0,0,0,0,0,0,0,0};
|
and i make some code after this.
in a certain point in the main function i need to rewrite the values. how to make it?
i tried this but doesn't work:
Code: |
in_buffer[] = {3,45,48,48,49};
|
and if something like this is possible, can i just write some of the values, like the first 5 of the array???
thanks! |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat May 13, 2006 10:46 am |
|
|
Try this:
Code: |
in_buffer[0] = 3;
in_buffer[1] = 45;
in_buffer[2] = 48;
in_buffer[3] = 48;
in_buffer[4] = 49;
|
There are other ways of doing this but a lot depends on what you are actually trying to accomplish. |
|
|
cjusto
Joined: 26 Apr 2006 Posts: 38 Location: Portugal
|
|
Posted: Sat May 13, 2006 11:08 am |
|
|
hi mark.
i didn't say that. but i want something else.
what u are telling me i know i can do.
i forgot to say that i already did that.
thanks anyway.
i really want sometthing to change the array in one line. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 13, 2006 11:54 am |
|
|
Quote: | i really want sometthing to change the array in one line. |
Then just write a macro to do it. Example:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
int8 in_buffer[80];
#define load_buffer(x0, x1, x2, x3, x4) \
in_buffer[0] = x0; \
in_buffer[1] = x1; \
in_buffer[2] = x2; \
in_buffer[3] = x3; \
in_buffer[4] = x4;
//======================================
void main()
{
load_buffer(3,45,48,48,49);
while(1);
} |
|
|
|
carmarmu
Joined: 09 May 2006 Posts: 15 Location: Valencia (Spain)
|
Re: changing an array in the main |
Posted: Sat May 13, 2006 12:44 pm |
|
|
When you declare an array, and you don�t put the dimension into brackets, the compiler automatically assigns the dimension of the components that you put after the symbol �=�.
In order to change the value of each one of the elements, you will have to write as it is the position of the element that you want to change.
Remember that element �1� of the Array is the �0� for the compiler. Therefore, if sights your program, you have several failures.
For example....
If you first write in_buffer[10] = {0,0,0,0,0,0,0,0,0,0} it�s wrong to write later in_buffer[] = {3,45,48,48,49} because dimensions don�t be equal.
If you want to change only some values, use:
in_buffer[Z] = data; where Z is the position, and data, the data you want assign.
Regards.
cjusto wrote: | hi everybody!
i have a doubt.
when i declare an array like this (global variable):
Code: |
BYTE in_buffer[10] = {0,0,0,0,0,0,0,0,0,0};
|
and i make some code after this.
in a certain point in the main function i need to rewrite the values. how to make it?
i tried this but doesn't work:
Code: |
in_buffer[] = {3,45,48,48,49};
|
and if something like this is possible, can i just write some of the values, like the first 5 of the array???
thanks! |
_________________ **CaRmArMu** Valencia (Spain) |
|
|
cjusto
Joined: 26 Apr 2006 Posts: 38 Location: Portugal
|
|
Posted: Sat May 13, 2006 1:12 pm |
|
|
hi!!!
thanks for the answers.
i think this will work fine.
Quote: |
#define load_buffer(x0, x1, x2, x3, x4) \
in_buffer[0] = x0; \
in_buffer[1] = x1; \
in_buffer[2] = x2; \
in_buffer[3] = x3; \
in_buffer[4] = x4;
|
thanks!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|