Sign in to follow this  
Followers 0
jeb

redifining an array

7 posts in this topic

hello,

i have a c question :

i have a global array :                  unsigned char test[2][2];

each time a certain button is pressed, i want to reset my array to the default array

{{1,2},{2,2}}

(this is just a 2*2 example, the array in my code is much bigger).

int this case the following line doesn't work :

test = {{1,2},{2,2}};

i don't want to use a loop for this !

do i have to use pointers ?

thanx

jeb

Share this post


Link to post
Share on other sites

I've read something about sdcc wanting brackets around every entry, maybe try this:

test = {{{1},{2}},{{2},{2}}};

Wow that's ugly!

Share this post


Link to post
Share on other sites

hello,

no the brackets solution doesn't work !

the problem is maybe that i want to redefine the values of an array which has already been defined.

should i use pointers ?

for example if i have a global variable :

////  unsigned char test [3];

the use of this, in a function :

////  test = {8, 8, 8};

gives :

syntax error: token -> '{' ; column 9

thanx in advance!

Share this post


Link to post
Share on other sites

Hello,

I think that this way of intializing arrays is only valid when it's used while defining the variable. I don't think it's allowed to be used as this in the body of a function.

You could indeed use a pointer instead, have a collection of the constant tables as you shown and have the pointer to point on one of the constant tables.

Othewise, you can also define constant tables and use a memcpy like function to copy the constant table within your array.

Best regards,

Lall

Share this post


Link to post
Share on other sites

I think that this way of initializing arrays is only valid when it's used while defining the variable. I don't think it's allowed to be used as this in the body of a function.

I had a feeling that was it, but I didn't have the confidence to say it heheheh

Hey jeb what are you putting in this array? There might be a quick way, depending on what is going in there...

Share this post


Link to post
Share on other sites

AFAIK, You could always cheat...

test[0] = {1,2};

test[1] = {2,2};

;D

Share this post


Link to post
Share on other sites

If that compiles with SDCC PIC16 port,  I'll give you a lollypop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0