Pages: [1] 2   Go Down
Author Topic: 2 dimensional array of chars.  (Read 1255 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Sr. Member
****
Karma: 7
Posts: 436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I have a 1 dimensional array of chars

char command[20]; 

Which contains a set of chars which are zero terminated.

So

command[0]="H"
command[1]="E"
command[2]="L"
command[3]="L"
command[4]="O"
command[5]=(char)0

I can refer to that as command so Serial.println(command); works and prints HELLO

Now the problem:

If I have a 2 dimensional array of chars

char commands[20][5]; 

Which contains a set of chars which are zero terminated.

So

commands[0][0]="H"
commands[1][0]="E"
commands[2][0]="L"
commands[3][0]="L"
commands[4][0]="O"
commands[5][0]=(char)0
commands[0][1]="W"
commands[1][1]="O"
commands[2][1]="R"
commands[3][1]="L"
commands[4][1]="D"
commands[5][1]=(char)0

How can I refer to a group in that

Serial.println(commands[][0]);  obviously can't work
and Serial.println(commands[0]);  obviously can't work because it wouldn't know which element it was talking about......


Is there a way ????

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unless you've got very peculiar requirements, I'd say you've got your array indices the wrong way round.


commands[0][0]="H"
commands[0][1]="E"
commands[0][2]="L"
commands[0][3]="L"
commands[0][4]="O"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Sr. Member
****
Karma: 7
Posts: 436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If it makes it easier then I could swap them but they are basically just chars in a grid....
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

char commands[20][5];

should be

char commands[20][6];

to have place for the '\0'  Needed if you want to use println()

and single quotes!!!

commands[0][0]='H'
commands[0][1]='E'
commands[0][2]='L'
commands[0][3]='L'
commands[0][4]='O'
commands[0][5]=0



Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry - Rob is correct - I missed the double quotes.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Sr. Member
****
Karma: 7
Posts: 436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I just wrote the above using copy and paste in order to populate the array.  The array is populated ok at the moment so

if I have the array as per your spec (which just means swapping the indices round) how do I actually print HELLO?

Thanks for taking the time.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5377
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use them that way, but you need to use them like you entered them.

Code:
for(int x=0; x<20;x++)
{
   if(commands[x][0] == 0) break;
   Serial.print(commands[x][0]);
}

Now, when you look at the alternative, doesn't AWOL's plan sound better?
Logged

UK
Offline Offline
Sr. Member
****
Karma: 7
Posts: 436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry if there was any confusion here....

I will swap the indices round happily and the array does have the correct values in it...

the following code shows me that:


Code:
 Serial.println("\n>>>>>");
  for (i = 0; i < numcommands+1; i++) {
    Serial.print(i);
    Serial.print(" = '");
    for (int k = 0; k < 20; k++) {  
      Serial.print(commands[k][i]);
    }
    Serial.println("'");
  }
  Serial.println(">>>>>\n\n");

So with the indices swapped around which I will happily do....

can I refer to one string in the same way that I would if it were 1 dimensional

Code:
commands[0]='H'
commands[1]='E'
commands[2]='L'
commands[3]='L'
commands[4]='O'
commands[5]=0

Serial.println(commands)

Thanks
« Last Edit: May 04, 2012, 07:56:47 am by cowasaki » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you put proper code tags around that, it'll remove the italics, and start to make sense.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5377
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you switch the array dimensions around (and use the code tags) as AWOL suggests, then you can use these:
Code:
Serial.println(commands[0]);
Serial.println(commands[1]);
Logged

UK
Offline Offline
Sr. Member
****
Karma: 7
Posts: 436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you switch the array dimensions around (and use the code tags) as AWOL suggests, then you can use these:
Code:
Serial.println(commands[0]);
Serial.println(commands[1];


Thanks,  I will swap the indices around and check....
Logged

UK
Offline Offline
Sr. Member
****
Karma: 7
Posts: 436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks everyone for your help it is appreciated.

I was writing a parsing function which handles double quotes too.  It now works smiley

I'm new to C++ normally programming in assembler or Pascal (delphi)

Code:
char commands[5][20];

byte getwords(char s[30]){
  byte i=0;
  byte pos=0;
  byte numcommands=0;
  boolean inquotes=false;
  for (i = 0; i < 6; i++) {
    for (pos = 0; pos < 20; pos++) {
      commands[i][pos]=(char)0;
    }
  }
  pos=0;
  for (i = 0; i < 30; i++) {
    if (s[i]==NULL){break;}
    if (s[i]==(char)34){
      inquotes=!inquotes;
    } else {
      if ((s[i]==(char)32) && !inquotes){
        pos=0;
        numcommands++;
      } else {
        commands[numcommands][pos++]=s[i];
      }
    }
  }
  return numcommands+1;
}


I am trying to get my head round C++, is this written in the best way?  Can anyone see anything that is best done another way?

Thanks for looking.
« Last Edit: May 04, 2012, 10:16:14 am by cowasaki » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5377
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

BTW, there is a quick way to initialize that array.
Code:
char commands[2][16] = {
   "Hello ",
   "world!"
};

void setup() {
   Serial.begin(9600);
   Serial.print(commands[0]);
   Serial.println(commands[1]);
}

void loop() {
}
Logged

UK
Offline Offline
Sr. Member
****
Karma: 7
Posts: 436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,  I actually know that but I was trying to make sure it was very obvious what I was doing.

just looking for quick and/or smaller ways of doing stuff.

Code:
  for (int i = 0; i < 6; i++) {
    for (int k = 0; k < 20; k++) {
      commands[i][k]=(char)0;
    }
  }

As an example:      To empty the array, is there an easier/smaller way of doing this?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use memset, but if all you're using is string functions (strcmp, strcat, strlen etc) then all you need to do is set the first element of each string to null '\0'.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1] 2   Go Up
Jump to: