|
|
View previous topic :: View next topic |
Author |
Message |
rwskinner
Joined: 08 Dec 2006 Posts: 125 Location: Texas
|
Newbie - Assigning values to array during runtime |
Posted: Sun Dec 10, 2006 3:41 pm |
|
|
int buffer[8]
Can I assign values at run time like this?
buffer = {1,2,3,4,5,6,7,8};
I do I have to do it long hand
buffer[0] = 1;
buffer[1] = 2;
....
Sorry, new to C and I know I can set the values during compile time when I declare the var but I didn't know of an easy way during runtime. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 10, 2006 3:59 pm |
|
|
Quote: |
Can I assign values at run time like this?
buffer = {1,2,3,4,5,6,7,8};
do I have to do it long hand
buffer[0] = 1;
|
You could copy a constant array to the RAM buffer with the memcpy()
function. I tested this with PCH vs. 3.249 and it works. Here's the
output of the program shown below:
Quote: |
01 02 03 04 05 06 07 08
|
Code: |
#include <18F452.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
char const data[8] = {1,2,3,4,5,6,7,8};
//======================================
main(void)
{
int8 i;
int8 buffer[8];
// Copy the constant array to the RAM array.
memcpy(buffer, data, sizeof(data));
// Display the contents of the RAM array.
for(i = 0; i < sizeof(buffer); i++)
printf("%X ", buffer[i]);
while(1);
} |
|
|
|
|
|
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
|