Go Down

Topic: 2 dimensional array of chars. (Read 1 time) previous topic - next topic

jraskell


but they are basically just chars in a grid....


That is actually incorrect.

Multidimensional arrays are only multidimensional in concept.  In actuality, they are stored in memory as a single dimensional array, with some indexing math being handled for you behind the scenes to give the appearance of multidimensionality.

cowasaki

#16
May 04, 2012, 06:00 pm Last Edit: May 04, 2012, 06:04 pm by cowasaki Reason: 1


but they are basically just chars in a grid....


That is actually incorrect.

Multidimensional arrays are only multidimensional in concept.  In actuality, they are stored in memory as a single dimensional array, with some indexing math being handled for you behind the scenes to give the appearance of multidimensionality.


I think the word "basically" is enough there!

You get a value from the grid by giving it the 2 indices.  The important thing I didn't know was that IF they were indexed the other way then it would have worked.  This was the missing bit of information which people helped with and I thank them for that. I did explain my background is not in C++ but assembler and Pascal.  Some of C's concepts appear a little strange unless you are used to them.

michael_x

Actually, seeing memory as a one-dimensional series of storage bytes is a concept as well.
We do not want to know how multiplexing and adressing is handled behind the scenes ;)

BTW: if the lengths of those strings is very different, you might benefit from
Code: [Select]
char* commands[2] = { "Hi", " world !!!!!"};

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

void loop() {
   commands[1][1] == 'w'; // this is true and valid
}


In this case, you really do not have a 2 dimensional grid.

jraskell


The important thing I didn't know


That is directly tied to my statement.

Understanding the 'actuality' of how arrays work (of any dimension) is the same as knowing that important thing you didn't know.


Actually, seeing memory as a one-dimensional series of storage bytes is a concept as well.


Actually, it is not.  Memory IS a one-dimensional series of storage bytes with linear addressing, even at the hardware level.  Nothing conceptual about that.

Do you know what the difference is between:
Code: [Select]
char* const chrptr = (char*)malloc(2);
and
Code: [Select]
char array1[2];

It's really nothing more than a syntactically different declaration.  Functionally, they're identical because you can use them both in exactly the same manners.  The following code is perfectly valid:
Code: [Select]

  chrptr[0] = 't';
  chrptr[1] = 0;
 
  *array1 = 't';
  *(array1+1) = 0;


Arrays ARE const pointers to a block of memory (and thus can also be treated as const pointers).
And conversely, const pointers to blocks of memory can be treated exactly like Arrays.


We do not want to know how multiplexing and adressing is handled behind the scenes


That's unfortunate.  There are definite benefit to having such knowledge.  For example, if I were to give you this:
Code: [Select]

char array[10][10] =
  {
    "asdfqwera",
    "qadwdghsd",
    "qsdvthefd",
    "kdiejksuw",
    "djuekwjsd",
    "dujehsgcd",
    "wujdhskdf",
    "dujwhajsv",
    "dujwkdidj",
    "idkwjushc"
  };


And ask you to count the number of 's' in that array, an array syntax method would look something like this:

Code: [Select]

  int count = 0;
  for(int x = 0; x < 10; x++)
    for(int y = 0; y < 10; y++)
    {
      if(array[x][y] == 's') count++;
    }


But this, would also be a valid method, utilizing a pointer and pointer arith:

Code: [Select]

  int count = 0;
  char* p = (char*)array;
  for(int x = 0; x < 100; x++)
    if(*p++ == 's') count++;


There is more than just a syntactic difference there.  The latter method utilizing pointers is about 20% faster than the former method using array indexes.

cowasaki

I am picking C++ up but I've basically not used C until recently.  I grew up with assembler and Pascal with BASIC years before that, I've been programming since the VIC20! in 1982 :)

There are lots of things that I'm picking up as I go along.  char arrays are something that Pascal DID use but all recent versions use more powerful "strings".

Manipulation of "char arrays" C++ style is my biggest headache at the moment.  The Arduino sub-set of commands does not include the ones I would pick to use out of the full implementation of C++ I think....

This is my current mental work out :)

I have two char arrays

commands[0] = "WRITE"

whilst

commands[1] = either an integer stored as a char array OR a "%" sign followed by a number with the number referring to an integer array  nums[100]

SO

I need to take %0 to %65535 and remove the "%" and get the value of the rest of it so 0 to 100.

In BASIC for example I could use: a=INT(mid(s$,2,len(s$)-1)) whilst in Pascal something similar and in assembler I'd work backwards subtract ASC"0" and multiply each number by it's column so units, tens, hundreds etc....


What would peoples take be on this as a code snippet using C++ ??

SurferTim

I use sprintf for this stuff. This puts the integer variable 'a' converted to text in commands[1].
Code: [Select]
int a = 24;
sprintf(commands[1],"%d",a);
Serial.println(commands[1]);


AWOL

I can imagine sscanf or atoi helping, but not really sprintf.
Can you explain, SurferTim?
"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.

cowasaki


I use sprintf for this stuff. This puts the integer variable 'a' converted to text in commands[1].
Code: [Select]
int a = 24;
sprintf(commands[1],"%d",a);
Serial.println(commands[1]);




I'll have a look at that. I have about 80 bytes of space left :)

So we'll see what it takes.

Go Up