S Suraj143 Active Member Aug 23, 2016 #1 I have 5o arrays & each it has 10 elements.Is there any easy way of calling this array one by one when a button pressed? Ex;when the button press call Msg1, press again call Msg2...etc... Code: char Msg1[] = {1,2,3,4,5,6,7,8,9,10}; char Msg2[] = {5,2,6,8,5,6,7,4,9,5}; char Msg3[] = {5,2,3,5,5,6,7,8,9,4}; char Msg4[] = {6,6,3,4,5,6,7,8,9,10}; char Msg5[] = {4,2,3,5,5,4,5,8,9,4}; char Msg6[] = {1,6,3,6,5,6,7,6,9,10}; ---- char Msg50[] = {1,6,3,4,5,6,7,8,9,6}; I use PIC16F886 with MikroC Compiler.
I have 5o arrays & each it has 10 elements.Is there any easy way of calling this array one by one when a button pressed? Ex;when the button press call Msg1, press again call Msg2...etc... Code: char Msg1[] = {1,2,3,4,5,6,7,8,9,10}; char Msg2[] = {5,2,6,8,5,6,7,4,9,5}; char Msg3[] = {5,2,3,5,5,6,7,8,9,4}; char Msg4[] = {6,6,3,4,5,6,7,8,9,10}; char Msg5[] = {4,2,3,5,5,4,5,8,9,4}; char Msg6[] = {1,6,3,6,5,6,7,6,9,10}; ---- char Msg50[] = {1,6,3,4,5,6,7,8,9,6}; I use PIC16F886 with MikroC Compiler.
Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member Aug 24, 2016 #2 Welcome to pointers!! There is an easy way.. C: char Msg[50[10]; char Msg[1][10] = {1,2,3,4,5,6,7,8,9,10}; char Msg[2][10] = {5,2,6,8,5,6,7,4,9,5}; char Msg[3][10] = {5,2,3,5,5,6,7,8,9,4}; char Msg[4][10] = {6,6,3,4,5,6,7,8,9,10}; char Msg[5][10] = {4,2,3,5,5,4,5,8,9,4}; char Msg[6][10] = {1,6,3,6,5,6,7,6,9,10}; ---- char Msg[50][10] = {1,6,3,4,5,6,7,8,9,6}; Or in my view the easiest way is pointer increment C: char * Msg[][] = {1,2,3,4,5,6,7,8,9,10, 5,2,6,8,5,6,7,4,9,5, 5,2,3,5,5,6,7,8,9,4, 6,6,3,4,5,6,7,8,9,10, ---- 1,6,3,4,5,6,7,8,9,6}; Then access is... C: int index=0; char * MsgPointer; MsgPointer = Msg +(index*10); //print routine to print next 10 chars..
Welcome to pointers!! There is an easy way.. C: char Msg[50[10]; char Msg[1][10] = {1,2,3,4,5,6,7,8,9,10}; char Msg[2][10] = {5,2,6,8,5,6,7,4,9,5}; char Msg[3][10] = {5,2,3,5,5,6,7,8,9,4}; char Msg[4][10] = {6,6,3,4,5,6,7,8,9,10}; char Msg[5][10] = {4,2,3,5,5,4,5,8,9,4}; char Msg[6][10] = {1,6,3,6,5,6,7,6,9,10}; ---- char Msg[50][10] = {1,6,3,4,5,6,7,8,9,6}; Or in my view the easiest way is pointer increment C: char * Msg[][] = {1,2,3,4,5,6,7,8,9,10, 5,2,6,8,5,6,7,4,9,5, 5,2,3,5,5,6,7,8,9,4, 6,6,3,4,5,6,7,8,9,10, ---- 1,6,3,4,5,6,7,8,9,6}; Then access is... C: int index=0; char * MsgPointer; MsgPointer = Msg +(index*10); //print routine to print next 10 chars..
S Suraj143 Active Member Aug 24, 2016 #3 Hi thanks for the codes.I have never used pointers before Note that I'm using PIC16F886 & it does not have much memory so I'm going to use constant arrays.When using const will it be able to use pointers ? No addresses I believe in program memory
Hi thanks for the codes.I have never used pointers before Note that I'm using PIC16F886 & it does not have much memory so I'm going to use constant arrays.When using const will it be able to use pointers ? No addresses I believe in program memory
Daniel Wood Member Aug 24, 2016 #4 Simply change the arrays to a single multidimensional array and reference each list of 10 items using.. msg["message number"]["item in message"] Code: #include <stdio.h> #define ROW_INDEX 4 #define COL_INDEX 10 #define ROW_MAX_INDEX (ROW_INDEX -1) #define COL_MAX_INDEX (COL_INDEX -1) char msg[ROW_INDEX][COL_INDEX] = { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, {20, 21, 22, 23, 24, 25, 26, 27, 28, 29}, {30, 31, 32, 33, 34, 35, 36, 37, 38, 39} }; void SomeFunction(char*); int main() { int row_index; /* Scan through each row of messages */ for(row_index = 0; row_index <= ROW_MAX_INDEX; row_index++) { /* Print Message index */ printf("Message %d\n",row_index); /* Pass the start address(&) of one 10-byte message to SomeFunction() */ SomeFunction(&msg[row_index][0]); } return 0; } void SomeFunction(char* local_msg) { int item_index; /* Scan through each item in a message */ for(item_index = 0; item_index <= COL_MAX_INDEX; item_index++) { /* Print the contents item */ printf("%d\t",*(local_msg + item_index)); } printf("\n\n"); } The output looks something like this Code: Message 0 0 1 2 3 4 5 6 7 8 9 Message 1 10 11 12 13 14 15 16 17 18 19 Message 2 20 21 22 23 24 25 26 27 28 29 Message 3 30 31 32 33 34 35 36 37 38 39
Simply change the arrays to a single multidimensional array and reference each list of 10 items using.. msg["message number"]["item in message"] Code: #include <stdio.h> #define ROW_INDEX 4 #define COL_INDEX 10 #define ROW_MAX_INDEX (ROW_INDEX -1) #define COL_MAX_INDEX (COL_INDEX -1) char msg[ROW_INDEX][COL_INDEX] = { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, {20, 21, 22, 23, 24, 25, 26, 27, 28, 29}, {30, 31, 32, 33, 34, 35, 36, 37, 38, 39} }; void SomeFunction(char*); int main() { int row_index; /* Scan through each row of messages */ for(row_index = 0; row_index <= ROW_MAX_INDEX; row_index++) { /* Print Message index */ printf("Message %d\n",row_index); /* Pass the start address(&) of one 10-byte message to SomeFunction() */ SomeFunction(&msg[row_index][0]); } return 0; } void SomeFunction(char* local_msg) { int item_index; /* Scan through each item in a message */ for(item_index = 0; item_index <= COL_MAX_INDEX; item_index++) { /* Print the contents item */ printf("%d\t",*(local_msg + item_index)); } printf("\n\n"); } The output looks something like this Code: Message 0 0 1 2 3 4 5 6 7 8 9 Message 1 10 11 12 13 14 15 16 17 18 19 Message 2 20 21 22 23 24 25 26 27 28 29 Message 3 30 31 32 33 34 35 36 37 38 39