I've just started with C and I'm trying to understand the basics. Plenty of tutorials will tell you things and have you believe it without any real explanation and there are no answers on that I can find that are human readable.
In the following:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int *a;
a = malloc(5 * sizeof(int));
a[2] = 4;
printf("%d\n", a[0]); // Prints 0
printf("%d\n", a[2]); // Prints 4
return 0;
}
I have not explicitly declared int *a
as a pointer to an array, but if I allocate it some memory, I can then use a
like I had declared it as an array. Is declaring a pointer with square brackets just a shortcut for what I've done below? Are square brackets actually doing some pointer arithmetic?
Cheeky Second question
Why is the memory address assigned to a
and not *a
?