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

Filling an array without loops

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



Joined: 25 Feb 2007
Posts: 4

View user's profile Send private message

Filling an array without loops
PostPosted: Mon Mar 12, 2007 5:09 pm     Reply with quote

Hello. I was wondering if anyone knows a way I can fill an array multiple times without looping through all of the elements.

Basically, what I would like to do is this:

Code:
   int8   mode_data[8] = { 0, 2, 2, 1,5 , 2, 2, 5 };
   int8   mode_data[8] = { 2, 3, 1, 4, 2, 6, 4, 7 };


But you only declare an array like that once.

Is there any way I can change all of the data in an array at the same time with one command, rather than building a special function fill_array() or something?

Thanks!
Guest








PostPosted: Mon Mar 12, 2007 6:19 pm     Reply with quote

Well, you could create several arrays and use a pointer to switch among them. But you said you don't want to declare more arrays. It would be the fastest, though.

Look up memcpy() in the compiler manual. This lets you copy a block of data. Thing is, even if you don't have to use a loop, the compiler will use a library routine uses a loop anyway.

Depends what optimization or tradeoff you're really trying to get with this.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 12, 2007 10:39 pm     Reply with quote

See this thread for an example of loading an array with constant data
by using memcpy:
http://www.ccsinfo.com/forum/viewtopic.php?t=29142
yadobaka



Joined: 25 Feb 2007
Posts: 4

View user's profile Send private message

PostPosted: Mon Mar 12, 2007 11:54 pm     Reply with quote

Thanks guys. memcpy worked great. What I ended up doing was declaring the all the data arrays, and then i declared one more empty array the same size as the data arrays. I then used memcpy as you guys suggested. The reason I was looking to do this:

I have a for loop that adds up different data depending on certain cases. I wanted to use a switch, but I didn't want each case to have its own for loop.

all systems go!
barryg



Joined: 04 Dec 2006
Posts: 41

View user's profile Send private message

PostPosted: Wed Mar 14, 2007 1:15 pm     Reply with quote

This code will switch arrays using pointers.
Code:
/* Pointer-to-arrays demo */

#include <16f877.h>

int test_odd[]  = { 1, 3, 5, 7, 9 } ;   // Define 3 similar arrays
int test_even[] = { 0, 2, 4, 6, 8 } ;
int test_pwr2[] = { 1, 2, 4, 8, 16} ;

void verify(int pp[])       // A function that uses a 5 element array
{
    int i;
    int a;

    for(i=0; i<5; i++) {
   a = pp[i];
   a += pp[i];         // Put a breakpoint here to look at value of a
   a=0;            // BTW this code is nonsense
    }
}

int main(void)
{
    int i;
    int * params[];

    params = &test_odd[0];      // proper way to specify address of array start
    verify(params);

    params = test_even;         // in C, array name is passed as its address,
    verify(params);         //  so this works, too.

    params = test_pwr2;
    verify(params);         // runs same test on each array

}
You would only need to do this if you had to eliminate the time spent copying the array. The reason I post this now is that I find the best time to learn about something is when you are right there in a concrete example where you could use it. It may help you or someone else.
barryg



Joined: 04 Dec 2006
Posts: 41

View user's profile Send private message

PostPosted: Wed Mar 14, 2007 1:23 pm     Reply with quote

And here is a cut-down version of the same thing. I think it's clearer if you start from the first example, and then see that this works, too. This example eliminates the extra pointer variable which isn't necessary, but helps understanding the mechanism.
Code:
/* Pointer-to-arrays demo */

#include <16f877.h>

int test_odd[]  = { 1, 3, 5, 7, 9 } ;   // Define 3 similar arrays
int test_even[] = { 0, 2, 4, 6, 8 } ;
int test_pwr2[] = { 1, 2, 4, 8, 16} ;

void verify(int params[])       // A function that uses a 5 element array
{
    int i;
    int a;

    for(i=0; i<5; i++) {
   a = params[i];
   a += params[i];         // Put a breakpoint here to look at value of a
   a=0;
    }
}

int main(void)
{
    int i;

    verify(test_odd);
    verify(test_even);         // run test again
    verify(test_pwr2);
}


I ran these using MPLAB-SIM to verify that they compile and work. In order to get a look at the value of "a" in the function, you have to take out the "int a" declaration and put it outside the function to make it global. Maybe the CCS debugger is smarter Neutral

The mechanism is not limited to fixed length arrays, but as you get more clever you also risk stomping your memory, so you have to be careful.

Barry
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