go away spammer

Author Topic: Array Quandary  (Read 1633 times)

0 Members and 1 Guest are viewing this topic.

Offline tzankoffTopic starter

  • Robot Overlord
  • ****
  • Posts: 122
  • Helpful? 0
Array Quandary
« on: April 14, 2011, 12:26:26 PM »
A while back, I had asked for Webbot's help on how to store numbers typed in HyperTerminal and send the collected data to an MP3Trigger. Some of the "plain English" code was as follows:

Code: [Select]
if( got a byte from the uart ){
     read the byte
     if it is '0'..'9' then
          uint8_t val = char - '0'; // Change from ascii to a number
          trackNum = trackNum *10 + val; // Keep the 'total' in trackNum
          ...

It works great and the code is still in use, but I was wondering if there was a "saving characters" equivalent or if there was someway of accomplishing the following.

Code: [Select]
__?__ varX; // not sure what type to declare (see below)
__?__ varY;
char mapPoints[2][4][2]={{"A1","A2","A3","A4"},{"B1","B2","B3","B4"}}; // maybe not the most proper but
     technically valid from what I have read. I know this is not so much an "array of strings" as it is an "array of characters".

for (x=0; x<=3; x++) // the A array elements
     {
     // assign 'A#' to varX;
     // do stuff

     for (y=0; y<=3; y++) // the B array elements
          {
          // assign 'B#' to varY;
          // do stuff

          if (condition)
               {
               // do something
               ]

          }

     next B
     }

next A

The problem I am having is assigning multi-character values to varX and varY. Using "wchar_t" does not seem to work like I was led to believe it should. Various joining methods (strcat, append, superGlue) do not seem to work. Any ideas?

paulstreats

  • Guest
Re: Array Quandary
« Reply #1 on: April 15, 2011, 01:15:27 AM »

Quote
The problem I am having is assigning multi-character values to varX and varY

To store multiple characters you would be best off at the minute using an array rather than just a plain variable.

Code: [Select]
char varX[2];

varX[0] = 'A';
varX[1] = '1';


There are other ways such as using Integers, where the value can be seen as an integer value or a double byte value (requiring some conversion) or create you own Type Definition.

I assume that you are wanting to store sensor values in a grid array (where varX and varY are grid location flags).

 You might want to just use a plain 2d array instead:

Code: [Select]
char map[10][10];

So accessing map[1][8] would be the equivalent of accessing "B7" (or B8 depending on whether you use 0 as 1).


you can then use a plain variable for X and Y

 


Get Your Ad Here

data_list