So I was just playing around with Array pointers to see how they work, my example below allocates space for 2 array pointers to an array with 10 ints.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int (*p)[10] = malloc(sizeof(int[10]) * 2);
for (int arrayI = 0; arrayI < 2; ++arrayI) {
for (int i = 0; i < 10; ++i) {
p[arrayI][i] = (arrayI+1) * i;
}
}
for (int arrayI = 0; arrayI < 2; ++arrayI) {
for (int i = 0; i < 10; ++i) {
printf("%d\n", p[arrayI][i]);
}
printf("\n");
}
}
This seems to work fine and gives me:
C:\Users\USERNAME\Desktop>gcc -Wall -Wextra --std=c18 a.c && a.exe
0
1
2
3
4
5
6
7
8
9
0
2
4
6
8
10
12
14
16
18
For my question, you rarely see code like this, if at all. Is there anything dangerous with doing things like this or is it just "bad code". And again, this is just me playing around with Array pointers.