Pages: 1 [2]   Go Down
Author Topic: 2 dimensional array of chars.  (Read 1385 times)
0 Members and 1 Guest are viewing this topic.
New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


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

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.
« Last Edit: May 04, 2012, 11:04:49 am by cowasaki » Logged

Germany
Online Online
Faraday Member
**
Karma: 56
Posts: 2972
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink

BTW: if the lengths of those strings is very different, you might benefit from
Code:
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.
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char* const chrptr = (char*)malloc(2);
and
Code:
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:
  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:
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:
  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:
  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.
Logged


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

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 smiley

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 smiley

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++ ??
Logged

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

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

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

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

I use sprintf for this stuff. This puts the integer variable 'a' converted to text in commands[1].
Code:
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 smiley

So we'll see what it takes.
Logged

Pages: 1 [2]   Go Up
Jump to: