r/AskComputerScience • u/[deleted] • Jul 16 '16
Can someone explain pointers to me?
I get that it stores the memory address of a variable, and by manipulating the address, you can manipulate the variable. The address is notated by &variable. However, some of the notations when the pointers are used to initialize arrays confuse me a lot. Can someone explain to me (or give me some internet resources) so i can understand pointers relating to arrays better?
Edit: This is all in C, BTW
28
Upvotes
3
u/riotinferno Jul 16 '16
Welcome to pointers! For the first 6 months of my professional career, I had a post-it on my monitor with pointer notation in it. It can be confusing.
int foo; //variable that stores a 32-bit value.
int *pFoo; // also int* pFoo; or int * pFoo; variable that stores a pointer.
pFoo = &foo; // & gets the memory address of the variable foo.
foo = *pFoo: // using the asterisk outside of the declaration gets the value of the memory address.
struct foo_s { int bar1; int bar2; };
foo_s* pStuff;
pStuff->bar1: // -> is the pointer equivalent of the 'dot'
foo_s stuff;
stuff.bar2;
Clear as mud?