CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Newbie - Assigning values to array during runtime

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rwskinner



Joined: 08 Dec 2006
Posts: 125
Location: Texas

View user's profile Send private message

Newbie - Assigning values to array during runtime
PostPosted: Sun Dec 10, 2006 3:41 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Dec 10, 2006 3:59 pm     Reply with quote

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);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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