//#include <string.h>
char** strings = NULL;
struct ENC {
char* l = NULL;
char* r = NULL;
};
ENC enc;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
String s="LEFT";
enc.l = (char*)malloc(sizeof(s.length()+1));
enc.l = s.c_str();
Serial.println(enc.l);
//--------------------------
int row=10;
// Allocate memory for the array of strings
strings = (char**)malloc(row * sizeof(char*));
if (strings == NULL) {
fprintf(stderr, "Memory allocation failed\n");
// return 1;
}
// Allocate memory for each string and assign values
for (int i = 0; i < row; i++) {
long rnd=random(0,1000000);
// Serial.print("\trnd:"); Serial.println(rnd);
String str = "-----" + String(rnd) + "---";
rnd=random(0,20);
for (int i = 0; i < rnd; i++) {
str+=i;
}
// Serial.print("\tstr:"); Serial.println(str);
// Allocate memory for each string
int len=str.length();
strings[i] = (char*)malloc((len + 1)* sizeof(char));
if (strings[i] == NULL) {
fprintf(stderr, "Memory allocation failed\n");
// return ;
}
// Assign values to each string
sprintf(strings[i], str.c_str());
}
// Print the strings present in the array
for (int i = 0; i < row; i++) {
printf("%s\n", strings[i]);
}
// Free memory for each string
for (int i = 0; i < row; i++) {
free(strings[i]);
}
// Free memory for the array of pointers
free(strings);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
/*
// C Program to illustrate how to create a dynamic array of
// strings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// define the maximum length of the string you will store in
// the array
#define MAX_STRING_LENGTH 20
int main()
{
// Initialize a double pointer to store the array of
// strings
char** strings = NULL;
// Declare the initial size of the dynamic array
int size = 5;
// Allocate memory for the array of strings
strings = (char**)malloc(size * sizeof(char*));
if (strings == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
// Allocate memory for each string and assign values
for (int i = 0; i < size; i++) {
// Allocate memory for each string
strings[i] = (char*)malloc((MAX_STRING_LENGTH + 1)
* sizeof(char));
if (strings[i] == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
// Assign values to each string
sprintf(strings[i], "Student%d", i);
}
// Print the strings present in the array
for (int i = 0; i < size; i++) {
printf("%s\n", strings[i]);
}
// Free memory for each string
for (int i = 0; i < size; i++) {
free(strings[i]);
}
// Free memory for the array of pointers
free(strings);
return 0;
}
*/