C array handling, i.e. passing to functions, 2D/multidimensional arrays, dynamic allocation, etc...
void process_2d_array_pointer(int (*array)[5][10]) {
// ...
}
process_2d_array_pointer(&a); // Use &a. a would decay to ptr to first elem.
// Definition and declaration
char dir[] = "/home/anni";
char http_request[BUFFER_SIZE];
// Length
int len = strlen(third);
// Split part of a string
char* first = strtok(string, " "); // ' ' = delimiter
char* second = strtok(NULL, " ");
char* third = strtok(NULL, "\r");
// Extern global char array
extern char arr[];
extern char* arr; // DOES NOT WORK!
void process_array(int (&array)[3]) {
// ...
}
int a[3] = {1,2,3};
process_array(a);
template <size_t rows, size_t cols>
void process_2d_array_template(int (&array)[rows][cols]) {
// ...
}
int a[2][3] = {{1,2,3}, {4,5,6}};
process_2d_array_template(a); // OR: process_2d_array_template<2,3>(a);
void process_pointer_2_pointer(int **array, size_t rows, size_t cols) {
// ...
}
int a[5][10] = { { } };
int *b[5]; // surrogate
for (size_t i = 0; i < 5; ++i) {
b[i] = a[i];
}
process_pointer_2_pointer(b, 5, 10);
void process_2d_array(int (*array)[10], size_t rows) {
// ...
}
// OR
void process_2d_array(int array[][10], size_t rows) {
// ...
}
process_2d_array(a, 5);
char arr[10];
printf( "%s\n", buffer ); // decayed into "pointer to char"
printf( "%p\n", &buffer ); // Not automatically decayed. Ptr to 10-element array of char
int *p = new int;
int *pa = new int[4];
int **pp = new int*[4];
delete p;
delete [] pa;
delete [] pp;