To shorten, my issue is simply understanding why would this code:
int main() {
typedef int* ASElement;
int zero = 0;
int one = 1;
int two = 2;
int three = 3;
ASElement *elements = (int**)malloc(4 * sizeof(ASElement));
*elements = (int*)malloc(4*sizeof(int));
*(elements) = &zero;
*(*(elements+1)) = one; //segementation fault here
printf("%d", *(*(elements+1)));
return 0;
}
not work?
*(*(elements+1)) = one;what doeselements+1point to?*(elements) = &zero;<- here you overwrite the allocation of the previous line, since*elementsand*(elements)are the same. What did you intend to do? perhaps*(*(elements)) = zero?*(elements) = &zerosimply make the first item of the *(elements) array the value of zero?