i broke sizeof() ?

so im using the standard sizeof() function to print out every character in my array

here is the code:

 String dataarr[67] = {"*", " ", ".", ",", ":", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

void loop(){
  print_charset();
}

void print_charset(){
  Serial.println("size = " + String(sizeof(dataarr)));
  for(int i = 0; i < sizeof(dataarr); i++){
    Serial.println("char " + String(i) + " = " + String(dataarr[i]));
  }
}

now i clearly defiend the array to be 67 items.
however the code returns 402 items.

here are some example items that i did not put in the array:

and just for interest the items i did put in:

as you can see, it pints out the first 67 characters perfectly fine. then skips 3 items in the array and spews out junk for the rest.

and the array is exactly 67 items

im pretty confused with this

sizeof() gives you the number of BYTES of memory the argument occupies, NOT the number of elements in an array. Doing sizeof() on an array of String objects is a rather pointless thing to do, since there is NO defined relationship between the number of bytes each object occupies and the number of length of any data items contained within the object.

Regards,
Ray L.

I hereby revoke your privileges to use String due to heinous crimes against memory. Spend some time getting to know your good friends char and string (null terminated char arrays).

Try adding this to your code:

Serial.print("The size of the String data type is: ");
Serial.println(sizeof(String));

I think that will explain the results you're seeing.

Ok now im twice as confised.
I used a an array as i want to be able to scamble the array at some point.
i used a String array beacuse i do not understand the char array.
example:
String arr[68] = …etc; works fine.
char arr[68] = …etc; does not work.
char *arr[68] - …etc; works the exact same as String arr but whats the point of the * ?

in my old piece of code i was using this:

    for(int j = 0; j < sizeof(dataarr); j++){ 
      if(dataarr[j] == message.substring(i, i+1)) {
      index = j; 
      j = sizeof(dataarr);}
      }

im re-wrighting it somewhat to better optimize it (running at 0.82 seconds and want it < 0/5 seconds per operation), I need to take a character and find its position in an array and then use that position to find a character in an array on the other side of the code. (using rf)

what would be an efficiant example on how to do this ? the limitation is i can only send numbers 0 - 255.

since arduino has no indexOf() function built for arrays i am fored to make a rather bad patch.

used a String array beacuse i do not understand the char array

Then get to understand it, String has no place in a small embedded system like an Arduino.

char *arr[68] - …etc; works the exact same as String arr but whats the point of the * ?

The point of the * is that it saves you memory. Even an empty String (or String array element) uses six bytes of RAM on an eight bit Arduino.

I think the most re-read chapters in my C book is on character arrays & (c)strings
char *arr[68] is an array of 68 pointers to char, I dont think you will need that.
An array name is also a pointer to that array.

so you can index through the array like this
mychar = *(array + 0) or mychar = *(array) //will give you the first char
mychar = *(array + 1) //will give you the next and so on.

use char dataarr[68]= {"* .,;abc…XYZ"); //you have to leave space for the null char at the end.
or char dataarr[68]={’*’,’ ‘,‘a’,‘b’…‘Y’,‘Z’,’\0’);
char charToFind;

//to find the position of the character
found=false;
size=68;
charToFind=‘X’;

for(i=0;i<size;i++) {

if (dataarr*==charToFind) {*

  • saveIdx=i; //this is the index of the character*
  • found=true;*
  • }*
  • } *
  • //evaluate finding index*
  • if(!found)*
  • Serial.println(“index not found”);*

It's a shame about the italics in your example but that is what you get when you use an array index of [­i] and no [­code] [­/code] tags

REPOST W/code tags – Thanks

I think the most re-read chapters in my C book is on character arrays & (c)strings
char *arr[68] is an array of 68 pointers to char, I dont think you will need that.

An array name is also a pointer to that array.
so you can index through the array like this, for example, but you don’t have to
mychar = *(array + 0) or mychar = *(array) //will give you the first char
mychar = *(array + 1) //will give you the next and so on.

use . . .

char dataarr[68]= {"* .,;abc.......XYZ"); //increased array by 1 you have to leave space for the null char at the end.

OR

char dataarr[68]={'*',' ','a','b'...'Y','Z','\0');

char charToFind;    //to find the position of the character

      found=false;
      size=68;
      charToFind='X';
      
      for(i=0;i<size;i++) {

          if (dataarr[i]==charToFind) {
              saveIdx=i;              //this is the index of the character
              found=true;
         }
      }    

      
      //evaluate finding index
       if(!found)
            Serial.println("index not found");
 if (dataarr == charToFind)

The array index, presumably [­i], has gone walkabout

noweare:

char dataarr[68]={'*',' ','a','b'...'Y','Z','\0');

char charToFind;    //to find the position of the character

found=false;
      size=68;
      charToFind=‘X’;
     
      for(i=0;i<size;i++) {

if (dataarr[i]==charToFind) {
              saveIdx=i;              //this is the index of the character
              found=true;
        }
      }

//evaluate finding index
      if(!found)
            Serial.println(“index not found”);

this is basically what i had with the string array, and one the problem is still there

char toFind = 'e';
byte Buffer[16] = {0}; 
char *dataarr[68] = {'a', 'b', 'c', 'd', 'e', 'f', ...etc}
for(int i = 0; i < size; i++){ if toFind == dataarr[]i { Buffer[0] = i;}}

also im using strings not chars for the inpit

string message = "hello world";
for(int j = 0; j< sizeof(message); j++)(
  toFind = message.substring(j, j+1);
  byte Buffer[16] = {0}; 
  char *dataarr[68] = {'a', 'b', 'c', 'd', 'e', 'f', ...etc}
  for(int i = 0; i < size; i++){ if toFind == dataarr[]i { Buffer[0] = i;}}

the point is with no knowlage of size how do i find it. sizeOf() wont return the array size fpr both char arrays or string arrays.

if i change the size of the array then i have to go through the code and change all the size flags.

Im looking for 1 of 2 answers to this.
1: how to get the array size using a function;
2: how to get the index of a value from an array without looping search through the array.

additional note, i cant use a string “abcdefg…etc” as i will be using “\r” “\n” and “” in the array.
i can only send a fixed size array and the string could be 5-6 characters shorter than the array i am sending. i need null characters for those cases
[/code]

Are you aware of strlen?

I think you have steered us down the path of an XY problem.
I'm guessing that what you are really wanting to do isn't that complicated but probably takes a bit more understanding of the C language than you currently have at this point.

If you described what you are really wanting to do, rather than asking for help with your presumed solution, you may get better help trying to solve your real problem.


Some additional comments

AceScottie:

char *dataarr[68] = {'a', 'b', 'c', 'd', 'e', 'f', ...etc}

That line is telling the compiler you are creating an array that contains pointers to data type char (That is what the star before dataarr means), but you are filling in the array with data of type char.
A pointer to a character and a character are very different.
'a' is a character, a single byte with a hex value 0x61
"a" is C string, When referenced like "a" it is a pointer (a memory address) that points to a C string which is 'a' that is followed by a null (zero) byte as all C strings end with a null/zero byte.

You need to go study up on C language specifically, arrays, pointers and C strings.

From some of your comments sounds like you are not wanting the size of the array, but perhaps the number of elements in the array. Those are not the same thing.
sizeof() will return the size of the array, while sizeof(foo)/sizeof(foo[0]) will return the number of elements in foo by taking the size of the array and dividing it by the size of each element of the array.
Using the the size of the 0th element to get the size of each element, since each element in an array is always the same size.

--- bill

sizeOf() wont return the array size fpr both char arrays

Try this

char dataarr[] = {'*', ' ', 'a', 'b', 'Y', 'Z', '\0'};

void setup()
{
  Serial.begin(115200);
  Serial.println(sizeof(dataarr) / sizeof(dataarr[0]));
}

void loop()
{
}

and this

char dataarr1[] = {'*', ' ', 'a', 'b', 'Y', 'Z', '\0'};
char dataarr2[] = {"* abYZ"};

void setup()
{
  Serial.begin(115200);
  Serial.println(sizeof(dataarr1) / sizeof(dataarr1[0]));
  Serial.println(sizeof(dataarr2) / sizeof(dataarr2[0]));
}

void loop()
{
}