| C Tutorial | Using pointers | |||
|
Data types in C
|
Declaring a pointer allocates space for the pointer itself, but it does not allocate space for the pointee. The pointer must be set to point to something before you can dereference it.
Here's some code which doesn't do anything useful, but which does demonstrate (1) (2) (3) for pointer use correctly...
int* p; // (1) allocate the pointer int i; // (2) allocate pointee struct fraction f1; // (2) allocate pointee
p = &i; // (3) setup p to point to i *p = 42; // ok to use p since it's setup
p = &(f1.numerator); // (3) setup p to point to a different int *p = 22;
p = &(f1.denominator); // (3) *p = 7;
So far we have just used the & operator to create pointers to simple variables such as i. Later, we'll see other ways of getting pointers with arrays and other techniques.
Want To Know more with Video ???
|
|