A pointer is an integer which designates the location in memory.
int* pA ; // declares a pointer to an integer in a pointer variable.
The position "*" is flexible. You can declare a pointer as such too:
int * pA or int *pA
The form int* is more natural as it is easy to call out the type of pA is int*
To initialize a pointer to NULL,
int *pA = 0 or int *pA = NULL
Note that *pA when used outside of declaration means the thing pointer pA points to. This allows you to access the item at the far end of the pointer and "*" is called dereferencing the pointer.
An object is implemented using pointer. However, you never dereferencing an object when used. You just use the name of the object.
Pointer of type void* is a generic pointer. It can point to anything. Effectively, pointer to void bypass type checking. Both array and string in C are pointers.
To obtain a pointer from a variable, use the address (&) operator. For example
int result = 0;
pA = &result;
int** ptr; // declare a pointer to a pointer
To create a pointer to a function, just use the function name without the parameters. For example
int square(int a, int b);
&square is the pointer to the function square
No comments:
Post a Comment