Pages: [1]   Go Down
Author Topic: error: invalid types 'char[int]' for array subscript  (Read 360 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I found this a while back not sure if it was originally written in c or c++... been trying to convert it to work with Arduino. I am just trying to make this bit work and will add some more in the future.

I keep getting the error: invalid types 'char[int]' for array subscript  on this line " encrypted += original[temp] ^ (int(key) + temp) % 255;"


Code:
char original = 'put me in a box, a box of worms';
char key = 'z';
char end;
char getKey;
int temp = 0;
String encrypted = "";
String unencrypt = "";

void setup(){
  Serial.begin(9600);
  end = original ^ key;
  getKey = original ^ end;
  Serial.println("Ready");
  delay(1000);

}
void loop(){
  Serial.print(original);
  c();
  delay(1000);
  d();
  delay(5000);
}
void c(){// add variable in future
  temp = 0;
  while(temp < sizeof(original)){
    encrypted += original[temp] ^ (int(key) + temp) % 255;
    temp++;
  }
  Serial.print("Encrypt: ");
  Serial.println(encrypted);

}
void d(){
  temp = 0;
  while(temp < sizeof(original)){
    unencrypt += encrypted[temp] ^ (int(key) + temp) % 255;
    temp++;
  }
  Serial.print("Decrypt: ");
  Serial.println(unencrypt);
}
Logged

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

The first bugs are is in the very first line of code, that should read
Code:
char original[] = "put me in a box, a box of worms";

read the pages about array's in C / C++ in the tutorial section, that should help you to fix the code



Logged

Rob Tillaart

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

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,
I actually found a better library that serves my purpose
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3080
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's probably a good idea to avoid using Strings.  Appending those characters to a String will use a lot of memory.
Logged

Pages: [1]   Go Up
Jump to: