Go Down

Topic: 2 dimensional array of chars. (Read 4530 times)previous topic - next topic

cowasaki

May 04, 2012, 02:29 pm
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......

[font=Verdana]Is there a way ?
[/font]

AWOL

#1
May 04, 2012, 02:34 pm
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"
"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.
I speak for myself, not Arduino.

cowasaki

#2
May 04, 2012, 02:38 pm
If it makes it easier then I could swap them but they are basically just chars in a grid....

robtillaart

#3
May 04, 2012, 02:41 pm
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

Rob Tillaart

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

AWOL

#4
May 04, 2012, 02:42 pm
Sorry - Rob is correct - I missed the double quotes.
"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.
I speak for myself, not Arduino.

cowasaki

#5
May 04, 2012, 02:44 pm
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.

SurferTim

#6
May 04, 2012, 02:44 pm
You can use them that way, but you need to use them like you entered them.

Code: [Select]
`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?

cowasaki

#7
May 04, 2012, 02:50 pmLast Edit: May 04, 2012, 02:56 pm by cowasaki Reason: 1
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: [Select]
`  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: [Select]
`commands[0]='H'commands[1]='E'commands[2]='L'commands[3]='L'commands[4]='O'commands[5]=0Serial.println(commands)`

Thanks

AWOL

#8
May 04, 2012, 02:52 pm
If you put proper code tags around that, it'll remove the italics, and start to make sense.
"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.
I speak for myself, not Arduino.

SurferTim

#9
May 04, 2012, 02:56 pm
If you switch the array dimensions around (and use the code tags) as AWOL suggests, then you can use these:
Code: [Select]
`Serial.println(commands[0]);Serial.println(commands[1]);`

cowasaki

#10
May 04, 2012, 02:57 pm

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

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

cowasaki

#11
May 04, 2012, 03:02 pmLast Edit: May 04, 2012, 05:16 pm by cowasaki Reason: 1
Thanks everyone for your help it is appreciated.

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

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

Code: [Select]
`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.

SurferTim

#12
May 04, 2012, 03:58 pm
BTW, there is a quick way to initialize that array.
Code: [Select]
`char commands[2][16] = {   "Hello ",   "world!"};void setup() {   Serial.begin(9600);   Serial.print(commands[0]);   Serial.println(commands[1]);}void loop() {}`

cowasaki

#13
May 04, 2012, 04:02 pm
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: [Select]
`  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?

AWOL

#14
May 04, 2012, 04:05 pm
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'.
"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.
I speak for myself, not Arduino.

Go Up