foo->bar is equivalent to (*foo).bar, i.e. it gets the member called bar from the struct that foo points to.
pointers - Arrow operator (->) usage in C - Stack Overflow
arrays - How do square brackets work in C? - Stack Overflow
String Enums in C# — When, Why, and How? : csharp
Basic String Encryption and Decryption in C# : csharp
Videos
foo->bar is equivalent to (*foo).bar, i.e. it gets the member called bar from the struct that foo points to.
Yes, that's it.
It's just the dot version when you want to access elements of a struct/class that is a pointer instead of a reference.
struct foo
{
int x;
float y;
};
struct foo var;
struct foo* pvar;
pvar = malloc(sizeof(struct foo));
var.x = 5;
(&var)->y = 14.3;
pvar->y = 22.4;
(*pvar).x = 6;
That's it!
I have not explicitly declared
int *aas a pointer to an array, but if I allocate it some memory, I can then usealike I had declared it as an array. Is declaring a pointer with square brackets just a shortcut for what I've done below?
Similar, but no. int *a declares a as pointer to int. int b[5] allocates space for 5 ints, declares b as constant pointer to int an array-of-int reference (which can in most cases be treated as a constant pointer to int), and defines b to be the pointer to the allocated space. Thus, int b[5] is doing way more than int *a, which also means int *a is more flexible than int b[5]. For example, you can increment a, but not b.
malloc allocates memory on heap. int b[5], if a global variable, will be in the program's data segment (i.e. it is literally compiled into the executable). If local, it will be allocated on stack. Again, similar, but different.
Are square brackets actually doing some pointer arithmetic?
In declaration, no. When you use pointer variables, yes: x[y] is identical to *(x + y). So a[1] is the same as *(a + 1), which is the same as *(1 + a), which is again the same as 1[a] (but please don't use this last one).
Why is the memory address assigned to
aand not*a?
Cheeky answer for a cheeky question: Because you wrote a = ... and not *a = ....
EDIT: John Bode gave a useful pointer (heh): constant pointers and array references are not the same thing, although they are pretty damn similar. For one thing, sizeof(...) will give a different result.
Are square brackets actually doing some pointer arithmetic?
Yes. Brackets can be applied to any pointer, not just arrays. They provide a shorthand for pointer arithmetic and pointer dereferencing. Your code is essentially doing this:
Copyint *a = malloc(5 * sizeof(int));
*(a+2) = 4;
printf("%d\n", *(a+0));
printf("%d\n", *(a+2));
Which is actually doing the equivalent of this:
Copyint *a = malloc(5 * sizeof(int));
*((int*)(((unsigned long)a)+(2*sizeof(int)))) = 4;
printf("%d\n", *((int*)(((unsigned long)a)+(0*sizeof(int)))));
printf("%d\n", *((int*)(((unsigned long)a)+(2*sizeof(int)))));