C
From Wiki2
strcpy strcat
arduino <syntaxhighlight> char* str = "Hello"; char dest[12];
strcpy( dest, str ); strcat( dest, ".txt" ); </syntaxhighlight>
arrray vs pointer
c (char) string functions
multidim char arrays
<syntaxhighlight>
- include <iostream>
- include <cstring>
using namespace std;
int main(int argc, char *argv[]) {
unsigned i;
// Declaration of the two-dimensional array // as a pointer to pointer // char** array_2D; unsigned ROWS = 10; unsigned COLUMNS = 10;
// Allocate "main" array // array_2D = new char*[ROWS];
// Allocate each member of the "main" array // for (i = 0; i < ROWS; ++i) array_2D[i] = new char[COLUMNS];
// Fill the 6th element with a string and // print it out // strcpy(array_2D[5], "Hey there"); cout << array_2D[5] << endl;
// Deletion is performed in reversed order. // Pay special attention to the delete[] // operator which must be used to delete // arrays (instead of the "simple" delete) // for (i = 0; i < ROWS; ++i) delete[] array_2D[i];
delete[] array_2D;
return 0;
} </syntaxhighlight>
dynamic arrays
creating 2D arrays at runtime
Two-dimensional arrays != double pointers
You almost certainly need dynamic memory allocation for this. You also want to deep copy the contents of the array - it's a non-static local variable, so it's invalid out of its scope. You can't do TYPE arr[sz]; return arr; as a consequence. <syntaxhighlight> const size_t width = 3; const size_t height = 5; TimeSlot tmpTimeSlot[width][height];
systemMatrix = malloc(width * sizeof systemMatrix[0]); for (int i = 0; i < width; i++) {
systemMatrix[i] = malloc(height * sizeof systemMatrix[i][0]); for (int j = 0; j < height; j++) { systemMatrix[i][j] = tmpTimeSlot[i][j]; }
}
---
or
---
char (*place)[columns] = malloc(rows * sizeof *place); place[2][3];
</syntaxhighlight>